Partition Labels

mediumGreedylast-occurrence map + extend window to its endSolve on LeetCode#763

Part of Practice: Clever Commitments

You are given a string s of lowercase letters. Split it into as many contiguous parts as possible, under one rule: each letter may appear in at most one part. In other words, if a letter shows up in a part, every occurrence of that letter in the whole string must be inside that same part. Return the list of part lengths, in order.

Constraints that matter: up to 500 characters and only lowercase letters, so a fixed-size array of 26 last-occurrence positions is all the bookkeeping you need. Maximizing the number of parts means cutting as early as the rule allows.

Examples

Input:
s = "ababcbacadefegdehijhklij"
Output:
[9, 7, 8]

The parts are "ababcbaca", "defegde", "hijhklij". Every "a", "b", "c" lives in the first part; cutting any earlier would strand a later occurrence.

Input:
s = "eccbbbbdec"
Output:
[10]

The letter "e" appears first and second-to-last, and "c" appears last, chaining the whole string into one part.

Input:
s = "abc"
Output:
[1, 1, 1]

No letter repeats, so every character can be its own part.

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.