Partition Labels

Medium#763

You get a string of lowercase letters. Cut it into as many parts as you can so that each letter appears in only one part, and return the lengths of the parts in order. Our example: "cabcdbefegfh" — the answer is [6, 5, 1]: "cabcdb", "efegf" and "h".

input
s: a string of lowercase English letters.
returns
Cut s into as many parts as you can so that each letter appears in one part only; return the lengths of the parts, in order.
constraints
  • 1 ≤ s.length ≤ 500
  • s has lowercase English letters only
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string} s
 * @return {number[]}
 */
function partitionLabels(s) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more greedy problems

Pattern “Greedy” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.