Partition Labels

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Partition Labels

Approach

First, record the last occurrence index of each character. Then scan left to right, extending the current partition end to the maximum last-occurrence of any character seen so far. When the current index equals the partition end, the partition is complete.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Greedy + Hash Map Lookup

Why It Works

A partition is valid when every character within it has its final occurrence inside that partition. Tracking the farthest last-occurrence ensures no character spans two partitions.

Updated Feb 2026