Problem 249
Partition Labels
Medium · 20 pts · String, Greedy, Two Pointers, Hash Table
Problem
You are given a string S. Split it into as many consecutive parts as possible so that each letter appears in at most one part: all copies of a letter must end up in the same part. Print the sizes of the parts, from left to right. First record the last position of every letter. Then scan the string while tracking the farthest last position of any letter seen in the current part. When the scan reaches that position, the part can end there.
Input
A single string S of lowercase letters.
Output
Print the part sizes, space-separated.
Constraints
1 <= |S| <= 500
Example input
ababcbacadefegdehijhklij
Example output
9 7 8
Explanation
The first part must reach the last a, at position 8, and contains only a, b and c, so it is ababcbaca (size 9). The next part is defegde (size 7), and the last is hijhklij (size 8). The sizes are 9 7 8.