Partition Labels
Partition Labels is LeetCode 763 (Medium). Partition the string s into as many parts as possible so that each letter appears in at most one part, and return the size of each part, left to right.
- The parts are contiguous and keep their order: joined back together they give
s. - The answer is a list of part lengths, not the parts themselves.
- 1 <= s.length <= 500
- s consists of lowercase English letters.
Intuition
Think of each letter as a span from its first position to its last. A cut is allowed only where no span crosses it, since a crossing span would put the same letter in two parts.
So scan left to right and keep end, the furthest last position of any letter in the current part. When the index reaches end, no letter in the part appears again later, so cut there. Each new letter can only push end further right, so the check is a single comparison per position.
"Each value may appear in only one segment" means every value's first-to-last span must sit inside one segment. Track the furthest span end and close a segment when the scan reaches it.
Approach
Before reading on, decide what you need to know about a letter the moment it enters a part, and how you would tell that a part can safely end at the current index. Aim for O(n) time.
Record each letter's last position
One pass stores last[ch], the final index of every letter. Once a part contains ch, it has to reach last[ch], so this table is the only lookup the main scan needs.
Stretch the end while scanning
Keep start and end for the current part. At each index i, set end = max(end, last[s[i]]). The max matters because a letter met earlier in the part may need to reach further than the current one.
Cut when the index reaches the end
If i == end, every letter in s[start..i] has its last copy at or before i. Append the size i - start + 1, then set start = i + 1 so the next part begins just after the cut.
Why the earliest cut gives the most parts
A cut after index k is valid only if no letter in s[0..k] appears after k. That holds exactly when the scan finds i == end, so the scan makes every valid cut there is. Any other valid split uses a subset of these cuts and so has fewer or equal parts.
Code in Python, C++ and Java
Common pitfalls
Using only the current letter's last index
end = last[ch]
end = max(end, last[ch])
An earlier letter can need a later end. For "abba", the b at index 1 would shrink the end from 3 to 2, giving [3, 1] and splitting the two as; the answer is [4].
Dropping the + 1 from the size
sizes.append(end - start)
sizes.append(end - start + 1)
Both start and end belong to the part. For "abc" the short version returns [0, 0, 0] instead of [1, 1, 1].
Starting the next part at the cut
start = end
start = i + 1
The letter at the cut already belongs to the finished part. For "abc" this returns [1, 2, 2], counting each boundary letter twice.
Edge cases
"eccbbbbdec"The first e reaches index 8 and the c at index 1 reaches index 9, so end only meets i at the last index. The answer is [10].
"abc"Every letter's last position is where it stands, so the scan cuts after each one and returns [1, 1, 1].
Complexity
s. The last-position table holds at most 26 entries, so it does not grow with n; the output list is not counted.Partition Labels FAQ
Is this a merge intervals problem?
It can be solved that way: make one interval [first, last] per letter, sort by start, merge the ones that overlap, and return the merged lengths. The scan does the same merge without building the intervals, because it meets letters in order of their first position.
What if the string can contain any character?
Swap the 26-slot array for a hash map from character to last index. Nothing else changes, and the time is still O(n).