Facebook Pixel

Partition Labels

You are given a string s of lowercase English letters. Partition it into as many parts as possible so that each letter appears in at most one part, and return a list of the size of each part.

A partition is valid only if no letter is split across two parts.

Input & Output
Input
s — a string of lowercase English letters
Output
the size of each part, in order from left to right
Example
Input
s = ababcbacadefegdehijhklij
Output
[9, 7, 8]
Explanation

The partition is "ababcbaca", "defegde", "hijhklij". Every letter stays inside a single part; splitting into more parts would break that.

Example
Input
s = eccbbbbdec
Output
[10]
Explanation

The last 'c' sits at the very end and 'e' recurs late too, so the whole string has to be one part.

Constraints
  • 1 <= s.length <= 500
  • s consists of lowercase English letters only

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro