Skip to content
BytePatterns

Split String Into Blocks

MediumGreedy#greedy#last-occurrence~20m

Problem

Cut a lowercase string into as many pieces as possible so that every letter appears in at most one piece. Joining the pieces back together must rebuild the original string exactly. Return the length of each piece, in order.

Examples

Input:  s = "abacdc"
Output: [3, 3]
Why:    "aba" holds every a and b, "cdc" holds every c and d
Input:  s = "abcabc"
Output: [6]
Why:    every letter reappears late, so no cut is legal
Input:  s = "xyz"
Output: [1, 1, 1]
Why:    edge case, no letter repeats so every character is its own piece

Hints

0 / 3

Stuck on the idea rather than the code? What Makes Greedy Work covers it.