Menu
CoddyTech

Partition Labels

MediumGreedyHashingpython iconjava iconcpp iconc iconjs icon+10

You get a string s of lowercase letters. Cut it into as many consecutive parts as you can so that every letter appears in one part only: if a letter shows up in a part, all of its copies are in that part. Return the lengths of the parts from left to right.

Function

partitionLabels(s: string) → integer-array
sstring
the string to cut, lowercase letters only
Returnsinteger-array
the length of each part, from left to right

Constraints

  • 1 ≤ s.length ≤ 5 × 104
  • s holds lowercase English letters only.
  • The parts keep their order and together make up all of s, so the lengths add up to s.length.

Examples

Input
s = "abacdcefe"
Output
[3, 3, 3]
Explanation
The a's sit at 0 and 2, the c's at 3 and 5 and the e's at 6 and 8, so the cuts fall after aba and after cdc. No part can be cut again, because each one starts and ends with the same letter.

lock icon+14 hidden tests on Submit

Reset code
def partitionLabels(s):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

s = "abacdcefe"

Expected

[3, 3, 3]