Partition Labels
Master this topic with zero to advance depth.
Partition Labels
You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.
Return a list of integers representing the size of these parts.
Visual Representation
s = "ababcbacadefegdehijhklij"
Letter 'a' appears at indices [0, 2, 6, 8]
If we take 'a', we MUST take everything up to index 8.
Inside [0, 8], we have 'b' (ends at 5), 'c' (ends at 7).
Max end index for characters in [0, 8] is index 8.
Partition 1: "ababcbaca" (Size 9)Examples
The partition is "ababcbaca", "defegde", "hijhklij".
Level I: Dynamic Search
Intuition
For every partition starting at i, we find the last occurrence of s[i]. This is our initial end. Then, for every character between i and end, we update end if that character's last occurrence is even further.
Thought Process
- Iterate
ifrom 0 to . - Start a new partition:
start = i,end = lastOccurrence(s[i]). - Loop
jfromitoend:end = max(end, lastOccurrence(s[j])).
- When
jreachesend, the partition is complete. Addend - start + 1to result. - Move
itoend + 1.
Pattern: Window Expansion
Detailed Dry Run
s = "abaccb"
- i=0 ('a'), end=2 ('a' at index 2). Partition: [0, 2]
- Check inner: j=1 ('b'), last 'b' is 5. Update end=5.
- Check inner: j=3 ('c'), last 'c' is 4. end stays 5.
- j reaches 5. Partition size = 5-0+1 = 6.
Level II: Merge Intervals
Intuition
Treat each character as an interval from its first occurrence to its last occurrence. Any overlapping intervals MUST be merged into a single partition. This reduces the problem to a standard interval merging task.
Thought Process
- Find
firstandlastoccurrence for each character 'a'-'z'. - Create a list of intervals
[first, last]for characters that appear in the string. - Sort intervals by start time.
- Merge overlapping intervals.
- The size of each merged interval is a partition length.
Pattern: Interval Merging
Detailed Dry Run
s = "abaccb" Intervals: a[0,2], b[1,5], c[3,4]
- Sorted: [0,2], [1,5], [3,4]
- Merge [0,2] and [1,5] -> [0,5] (overlaps)
- Merge [0,5] and [3,4] -> [0,5] (overlaps) Result: [6]
Level III: Optimal Greedy (One-Pass)
Intuition
As we scan the string, we keep track of the maximum last index seen so far for characters in the current partition. When our current index i catches up to this last index, we've found a valid partition.
Thought Process
- Store the
lastIndexfor every character in a map/array. - Maintain
startandendpointers. - Iterate
ifrom 0 to :end = max(end, lastIndex[s[i]]).- If
i == end:- Found partition! Size =
i - start + 1. - Update
start = i + 1.
- Found partition! Size =
Pattern: Greedy Boundary Tracking
Detailed Dry Run
s = "ababcbaca..."
| i | char | lastIndex[char] | end | Action |
|---|---|---|---|---|
| 0 | a | 8 | 8 | - |
| 1 | b | 5 | 8 | - |
| ... | ... | ... | 8 | - |
| 8 | a | 8 | 8 | i == end! Size = 8-0+1 = 9. start = 9. |
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.