मुख्य कंटेंट तक स्किप करें
Back to ChallengesPartition LabelsMedium 30 min

Partition Labels

You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.

Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.

Return a list of integers representing the size of these parts.

Examples

Input: s = 'ababcbacadefegdehijhklij'
Output: [9,7,8]
The partition is 'ababcbaca', 'defegde', 'hijhklij'. Each letter appears in at most one part.

Constraints

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

Complexity Analysis

Time
O(N)
two passes over the string of length N.
Space
O(1)
alphabet size is at most 26.

Test Cases

#1 Standard case
Input: s = 'ababcbacadefegdehijhklij'
Expected: [9,7,8]
#2 Single partition
Input: s = 'eccbbbbdec'
Expected: [10]
Output
Click "Run Code" to see output here...