Partition Labels
mediumGreedy+20 XPTime limit 2s
Split the string into as many parts as possible so that every letter appears in at most one part. Print the sizes of the parts, in order.
Example: ababcbacadefegdehijhklij → ababcbaca | defegde | hijhklij → 9 7 8.
Input: one line s (lowercase letters).
Output: the part sizes on one line.
Constraints: 1 ≤ length of s ≤ 10⁵.
Think about it: first record the last index of every letter. Then scan: the current part must extend at least to the last occurrence of every letter seen in it. When the scan index reaches that boundary, close the part.
Sample input 1
ababcbacadefegdehijhklij
Sample output 1
9 7 8
Sample input 2
eccbbbbdec
Sample output 2
10
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).