Skip to content
AI360Xpert
Beta
Difficulty: MediumGreedy

Partition Labels

Problem in Plain English

You are given a string and must split it into as many contiguous parts as possible so that no letter ever appears in more than one part. Return the length of each part in order.

Problem Statement

You are given a string s. Partition s into as many parts as possible so that each letter appears in at most one part.

The parts must be contiguous and, concatenated in order, must reconstruct s. Return a list of integers holding the size of each part.

Constraints

  • 1 <= s.length <= 500
  • s consists of lowercase English letters.

Examples

Example 1

Input
s = "ababcbacadefegdehijhklij"
Output
[9,7,8]

The parts are "ababcbaca", "defegde" and "hijhklij", with sizes 9, 7 and 8. Splitting into fewer parts, such as merging the first two, would still be valid but not maximal.

Example 2

Input
s = "eccbbbbdec"
Output
[10]

The letters e and c appear at both the start and the end, so the whole string is one part of size 10.

Example 3

Input
s = "a"
Output
[1]

One character is already one valid part. There is no boundary inside it, so the maximum number of parts is one.

Example 4

Input
s = "abacbdef"
Output
[5,1,1,1]

The first a forces the range through index 2, but b at index 1 reappears at 4 and extends it to abacb. Letters d, e and f each occur once and form separate parts. Cutting after aba would split the two b occurrences.

Intuition

Every occurrence of a letter must remain in one part. Seeing a at index 0 in Example 1 therefore forces the part through its last occurrence at 8. Letters discovered inside that forced range may extend it again. The first position where no encountered letter reaches farther is the earliest legal cut; taking every such cut produces as many parts as possible.

Approaches

Repeated Last Search

Solution Details

Reveal Repeated Last Search: intuition, complexity, and code

Hints

Hint 1
A part ending at index e must extend past the last occurrence of every letter inside it.
Hint 2
Knowing every letter final position in advance turns the repeated rescanning into one table lookup.
Hint 3
Sweep once while carrying the farthest last occurrence seen so far, and cut a part exactly when the sweep index reaches it.

Edge Cases

  • A single-character string yields [1]; the scan cuts as soon as its only index reaches end.
  • All distinct letters, such as "abc", split into all ones: [1, 1, 1].
  • A letter tying the first and last positions, like e in "eccbbbbdec", forces one part covering the whole string.
  • Nested or overlapping letter ranges, as in abacbdef, require repeated endpoint expansion before a cut.
  • At most 26 parts can be returned because each nonempty part must own a letter used by no other part.

Common Mistakes and Interview Tips

  • Cutting a part at the last occurrence of its first letter without extending for letters discovered inside the part.
  • Forgetting to move the part start to end + 1 after a cut, which double-counts the boundary character.
  • Using end - start instead of end - start + 1 loses the inclusive final character.
  • Assigning end to the current letter last position can shrink a boundary forced by an earlier letter; retain the maximum.
  • Scanning only to the original endpoint misses letters pulled in by later extensions.
  • Interview tip: proving each cut is valid is insufficient; show why it is the earliest possible cut and therefore maximizes the number of parts.

Key Takeaway

When each item pulls in everything linked to it, precomputing the farthest reach of every item turns repeated expansion into one greedy sweep.