Partition Labels

Medium

Topics
StringGreedyTwo PointersHash Table

You are given a string s. Partition it into as many parts as possible so that each letter appears in at most one part. Return a list of the sizes of these parts, in order.

Example 1

ababcbacadefegdehijhklij
No letter may appear in two parts, so a part cannot end before the last occurrence of every letter it contains.
Shades mark the partitions, left to right.
Input:  s = "ababcbacadefegdehijhklij"
Output: [9,7,8]

Example 2

eccbbbbdec
Input:  s = "eccbbbbdec"
Output: [10]

Constraints

  • 1 <= s.length <= 500
  • s consists of lowercase English letters.
Editorial — intuition, approach, complexity and pitfalls

Intuition

A part must extend at least until the last occurrence of every letter it contains. Track the farthest last-occurrence as you scan; when the current index reaches that boundary, the part is complete.

Approach

  1. Record the last index of each letter.
  2. Scan with a running end = max(end, last[s[i]]).
  3. When i == end, cut a part of length i - start + 1 and reset start.

Dry Run

s = "ababcbacadefegdehijhklij"
First pass records the LAST index of every character. Second pass walks forward,
carrying `end` as the furthest index any character seen in this block reaches:
  i=0   a, last a is 8     end 0 -> 8
  i=1..7                   end stays 8
  i=8   i == end           CUT, block length 9        res [9]
  i=9   d, last d is 14    end 8 -> 14
  i=10  e, last e is 15    end 14 -> 15
  i=11..14                 end stays 15
  i=15  i == end           CUT, block length 7        res [9, 7]
  i=16  h, last h is 19    end 15 -> 19
  i=17  i, last i is 22    end 19 -> 22
  i=18  j, last j is 23    end 22 -> 23
  i=19..22                 end stays 23
  i=23  i == end           CUT, block length 8        res [9, 7, 8]
answer 9 7 8
`end` is a running MAXIMUM over the block, not the last index of the current
character. That distinction is the whole algorithm: reaching i == end means
nothing before i reappears after i, which is exactly the condition for a cut.
Cut on last[c] instead and the splits come far too early -- this input returns
6 2 1 3 2 1 1 4 1 1 1 1 instead of 9 7 8.
The first pass is what makes the greedy second pass safe. Knowing every last
index up front means the cut decision at i is final and never needs revisiting,
which is why one forward walk suffices with no backtracking.

Complexity

  • Time — O(n): one pass to record last indices, one pass to partition.
  • Space — O(1): the last-index map holds at most 26 entries.

Common Pitfalls

  • The cut is at i == end, where end is the running maximum last-index over everything seen in this block -- not the last index of the current character. Cutting on last[c] splits far too eagerly, returning 6 2 1 3 2 1 1 4 1 1 1 1 where the answer is 9 7 8.
  • One pass to record last positions, one to walk. The first pass is what makes the second greedy choice safe.

Full Solution — all languages

class Solution:
    def partitionLabels(self, s):
        last={c:i for i,c in enumerate(s)}
        res=[]
        start=end=0
        for i,c in enumerate(s):
            end=max(end,last[c])
            if i==end:
                res.append(i-start+1)
                start=i+1
        return res
SensAI
Beta
Tap the mic to stop voice mode