FunctionRacerProblemsRace

Cut So Each Letter Stays Together

MediumGreedyPython, JavaScript, TypeScript, Java, C++

Cut s into as many contiguous pieces as possible so that no letter appears in two different pieces: every occurrence of a letter lands in the same piece. Return the lengths of the pieces in order from left to right. For the empty string return an empty list.

Example 1

s "ababcbacadefegdehijhklij"
returns [9,7,8]

The pieces are ababcbaca, defegde and hijhklij. Cutting ababcbaca any earlier would separate two copies of a, b or c.

Example 2

s "aabbcc"
returns [2,2,2]

aa, bb and cc each keep their letter to themselves.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Greedy problems