LeetCode #763 Medium

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.
Constraints
  • 1 <= s.length <= 500
  • s consists of lowercase English letters.
stringgreedyhash-table
Open on LeetCode ↗
02

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.

How to spot this pattern

"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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def partitionLabels(self, s: str) -> List[int]:
▶3 last = {ch: i for i, ch in enumerate(s)}
▶4 sizes = []
▶5 start = end = 0
▶6 for i, ch in enumerate(s):
▶7 end = max(end, last[ch])
▶8 if i == end:
▶9 sizes.append(end - start + 1)
▶10 start = i + 1
▶11 return sizes
a0b1b2a3c4d5d6c7e8abcdepart sizesnone yetrecord where each letter last appears
a3last position
b2last position
c7last position
d6last position
e8last position
One pass stores the last position of every letter. Each bar runs from a letter's first position to its last: once a part contains that letter, the part has to stretch to the end of its bar.
a0b1b2a3c4d5d6c7e8iendabcdepart sizesnone yet“a” last appears at 3 → end moves to 3
i0
last of a3
end3
A new part starts at 0. The part now holds “a”, whose bar reaches position 3. Cutting before that would split the “a”s across two parts, so the end moves out to 3.
a0b1b2a3c4d5d6c7e8iendabcdepart sizesnone yet“b” ends by 2 → end stays 3
i1
last of b2
end3
The bar for “b” stops at 2, which is inside the current end, so it asks for nothing new. The end stays at 3.
a0b1b2a3c4d5d6c7e8iendabcdepart sizesnone yet“b” ends by 2 → end stays 3
i2
last of b2
end3
The bar for “b” stops at 2, which is inside the current end, so it asks for nothing new. The end stays at 3.
a0b1b2a3c4d5d6c7e8iendabcdepart sizesnone yet“a” ends here → end stays 3
i3
last of a3
end3
This is the last “a” in the string, and no letter seen in this part needs to go further, so the end is still 3.
a0b1b2a3c4d5d6c7e8iabcdepart sizes4i reached the end → cut a part of size 4
size43 − 0 + 1
sizes[4]
Every letter in positions 0 to 3 has its last copy at or before 3, so nothing in this part shows up later. Cutting here is safe, and it is the earliest safe cut, which leaves the most room for more parts.
a0b1b2a3c4d5d6c7e8iendabcdepart sizes4“c” last appears at 7 → end moves to 7
i4
last of c7
end7
A new part starts at 4. The part now holds “c”, whose bar reaches position 7. Cutting before that would split the “c”s across two parts, so the end moves out to 7.
a0b1b2a3c4d5d6c7e8iendabcdepart sizes4“d” ends by 6 → end stays 7
i5
last of d6
end7
The bar for “d” stops at 6, which is inside the current end, so it asks for nothing new. The end stays at 7.
a0b1b2a3c4d5d6c7e8iendabcdepart sizes4“d” ends by 6 → end stays 7
i6
last of d6
end7
The bar for “d” stops at 6, which is inside the current end, so it asks for nothing new. The end stays at 7.
a0b1b2a3c4d5d6c7e8iendabcdepart sizes4“c” ends here → end stays 7
i7
last of c7
end7
This is the last “c” in the string, and no letter seen in this part needs to go further, so the end is still 7.
a0b1b2a3c4d5d6c7e8iabcdepart sizes44i reached the end → cut a part of size 4
size47 − 4 + 1
sizes[4, 4]
Every letter in positions 4 to 7 has its last copy at or before 7, so nothing in this part shows up later. Cutting here is safe, and it is the earliest safe cut, which leaves the most room for more parts.
a0b1b2a3c4d5d6c7e8iendabcdepart sizes44“e” ends here → end stays 8
i8
last of e8
end8
This is the last “e” in the string, and no letter seen in this part needs to go further, so the end is still 8.
a0b1b2a3c4d5d6c7e8iabcdepart sizes441i reached the end → cut a part of size 1
size18 − 8 + 1
sizes[4, 4, 1]
Every letter in positions 8 to 8 has its last copy at or before 8, so nothing in this part shows up later. Cutting here is safe, and it is the earliest safe cut, which leaves the most room for more parts.
a0b1b2a3c4d5d6c7e8abcdepart sizes441answer: 4, 4, 1
result[4, 4, 1]
The scan cut 3 parts, and no letter crosses a green line. Each cut was made as early as it could be, so no split gives more parts.
05

Common pitfalls

Using only the current letter's last index

✗ Wrong
end = last[ch]
✓ Right
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

✗ Wrong
sizes.append(end - start)
✓ Right
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

✗ Wrong
start = end
✓ Right
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.

06

Edge cases

One part covers the whole string, e.g. "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].

All letters different, e.g. "abc"

Every letter's last position is where it stands, so the scan cuts after each one and returns [1, 1, 1].

07

Complexity

Time
O(n)
Space
O(1)
Two passes over s. The last-position table holds at most 26 entries, so it does not grow with n; the output list is not counted.
08

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).