Partition Labels
Cut a string into the most pieces with no shared letters
DSA, Medium, Greedy
Which structure or algorithm, and what does it cost.
A string s holds lowercase letters. Cut it into consecutive pieces so that no letter appears in more than one piece. Make as many pieces as possible. Return the length of each piece, in order from left to right.
Examples
Constraints
- 1 <= length of
s<= 200,000. shas only the lowercase letters a to z.
Your answer must contain
- The simple method and why it is too slow; the best method and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time and O(1) extra memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Take the first letter. How far must its piece at least reach?
Hint 2 the approach
Every letter inside a piece pushes the end of the piece out to that letter's last position. Find the last position of each letter first.
Hint 3 the key step
Scan left to right with a running end, the largest last position of the letters seen in this piece. When the scan reaches it, cut.
Walkthrough
Full walkthrough brute force to best
Brute force
Try every way to cut the string: each of the n - 1 gaps is cut or not, so there are 2^(n-1) ways. For each one, check that no letter is in two pieces, and keep the valid cut with the most pieces. That takes about 2^(n-1) * n steps, so it fails for anything but tiny strings.
Best method
First record the last position of each letter. Then scan the string with a piece start and a piece end. At each letter, push end out to that letter's last position. When the scan position equals end, no letter in the piece appears later, so cut there and record the length. This is safe because the first letter of a piece forces the piece to reach its last occurrence, and each letter inside pushes it further, so no earlier cut is allowed. Once nothing inside reaches past the current position, cutting now is best: any piece that holds two such pieces would only lower the count.
Hand trace
Run on xyzxabbcacd. The last positions are x 3, y 1, z 2, a 8, b 6, c 9 and d 10. Positions 0 to 3 (x, y, z, x): the end stays 3. At position 3 the scan meets the end, so cut with length 4. Position 4 (a): the end becomes 8. Positions 5 and 6 (b, b): the end stays 8. Position 7 (c): the end becomes 9. Position 8 (a): the end stays 9. Position 9 (c): the scan meets the end, so cut with length 6. Position 10 (d): the end is 10, so cut with length 1. The answer is [4, 6, 1].
Code
def partition_labels(s):
last = {ch: i for i, ch in enumerate(s)} # last position of each letter
sizes = []
start = end = 0
for i, ch in enumerate(s):
end = max(end, last[ch]) # the piece must reach this letter's last spot
if i == end: # nothing inside reaches further: cut
sizes.append(end - start + 1)
start = i + 1
return sizesTime and memory
O(n) time, one pass to record the last positions and one to cut. O(1) memory, because the table holds at most 26 letters.
Common mistakes
- Cutting as soon as the first letter of the piece is done. The piece must reach the last occurrence of every letter inside it.
- Recording the first position of each letter instead of the last one.
- Forgetting to move the piece start to i + 1 after a cut. Every later length is then measured from 0, so xyzxabbcacd gives [4, 10, 11].
- Returning the cut positions instead of the piece lengths. Subtract the piece start to get a length.