Skip to content
BASA DSA

Partition Labels

MediumGreedy

Split s into as many nonempty contiguous parts as possible so that each letter occurs in only one part. Return the lengths of those parts in order.

Example 1

Input
S = "abacdeef"
Output
[3,1,1,2,1]

Explanation

a ties indices 0..2 together; c and d are separate, both e letters stay together, and f is separate.

Example 2

Input
S = "ababcbacadefegdehijhklij"
Output
[9,7,8]

Explanation

Last occurrences force ranges 0..8, 9..15 and 16..23 of lengths 9,7,8.

Example 3

Input
S = "aaaa"
Output
[4]

Explanation

Every a must remain in one part of length four.

Constraints

  • 1 <= len(s) <= 500
  • s contains lowercase English letters

Function contractpartitionLabelsArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…