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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code