Partition Labels

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

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

Return a list of integers representing the size of each part.

Note that the partition must cover the entire string, and the concatenation of all parts must equal the original string.

Examples

Example 1:

Input: s = "ababcbacadefegdehijhklij"

Output: [9,7,8]

Explanation: The partition is "ababcbaca", "defegde", "hijhklij". Each letter appears in at most one part. A partition like "ababcbacadefegde", "hijhklij" is valid but produces fewer parts.

Example 2:

Input: s = "eccbbbbdec"

Output: [10]

Explanation: Characters e, c, b, and d are interleaved such that the entire string must be one partition.

Example 3:

Input: s = "abcdef"

Output: [1,1,1,1,1,1]

Explanation: Every character is unique, so each character forms its own partition.

Constraints

  • 1 <= s.length <= 500
  • s consists of lowercase English letters
Source: Greedy Techniques pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle