This page uses the site's newer layout, so only the problem statement and the section outline survive in the public payload — open the original for the rest.
Greedy Algorithms
Partition Labels
medium
DESCRIPTION (inspired by Leetcode.com)
Given a string of lowercase letters, split it into as many segments as possible so that each character appears in exactly one segment. When you concatenate all segments, they should form the original string. Return a list of integers representing the length of each segment.
Example 1
Input:
s = "abacbcdd"
Output:
[6, 2]
Explanation: The character 'a' appears at indices 0 and 2, 'b' at 1 and 4, 'c' at 3 and 5, 'd' at 6 and 7. To keep all occurrences of each character together, we partition into "abacbc" (length 6) and "dd" (length 2).
Example 2
Input:
s = "eccbbbbdec"
Output:
[10]
Explanation: All characters are interleaved such that the entire string must be one segment.