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 integers representing the size of these parts.
The parts are contiguous pieces of s, and joining them in order gives back s.
Function Signature
def partition_labels(s: str) -> list[int]:
Rules
-
Every part is a non-empty contiguous substring, and every character of
s
belongs to exactly one part.
-
A letter that appears in one part must not appear in any other part.
-
Return the part sizes in left-to-right order. Only one partition reaches the maximum number of parts, so the expected output is unique.
Constraints
-
1 <= s.length <= 500
-
s
consists of lowercase English letters.
Examples
Input: s = "ababcbacadefegdehijhklij"
Output: [9, 7, 8]
The parts are "ababcbaca", "defegde" and "hijhklij". No letter appears in two of them, and cutting any of them further would separate two copies of some letter. A split such as "ababcbacadefegde", "hijhklij" is also valid, but it has fewer parts.
Input: s = "eccbbbbdec"
Output: [10]
The e at index 0 has another copy at index 8, and the c at index 1 has another copy at index 9, the last index. The whole string has to stay in one part.
Input: s = "caedbdedda"
Output: [1, 9]
The letter c occurs only at index 0, so it forms a part by itself. The a at index 1 occurs again at index 9, so everything from index 1 to the end is one part.