Partition Labels - Problem

You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.

For example, the string "ababcc" can be partitioned into ["abab", "cc"], but partitions such as ["aba", "bcc"] or ["ab", "ab", "cc"] are invalid.

Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.

Return a list of integers representing the size of these parts.

Input & Output

Example 1 — Basic Partitioning
$ Input: s = "ababcbaca"
Output: [4,5]
💡 Note: The string can be partitioned into "abab" and "cbaca". The first partition contains all 'a' and 'b' characters, while the second contains all 'c' characters and remaining 'a's.
Example 2 — Simple Case
$ Input: s = "eccbbbbdec"
Output: [10]
💡 Note: All characters appear throughout the string, so we cannot partition it further. The entire string must be one partition of length 10.
Example 3 — Maximum Partitions
$ Input: s = "abcdef"
Output: [1,1,1,1,1,1]
💡 Note: Each character appears only once, so we can partition into 6 parts, each containing exactly one character.

Constraints

  • 1 ≤ s.length ≤ 500
  • s consists of lowercase English letters

Visualization

Tap to expand
Partition Labels ProblemInput: "ababcbaca" → Output: [4, 5]Original StringababcbacaPartition BoundaryPartition 1Partition 2"abab""cbaca"Length: 4Length: 5Each letter (a, b, c) appears in exactly one partitionResult: [4, 5]
Understanding the Visualization
1
Input String
Given string with characters that may repeat
2
Find Partitions
Each letter must appear in exactly one partition
3
Output Sizes
Return the size of each partition
Key Takeaway
🎯 Key Insight: Track the furthest last occurrence position to determine where each partition can safely end
Asked in
Amazon 45 Google 35 Facebook 28 Microsoft 22
269.4K Views
High Frequency
~15 min Avg. Time
8.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen