Search
⌘K
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.

Building Intuition

Observaion

Pseudocode

Walkthrough

Solution

When to Use This Pattern

Purchase Premium to Keep Reading

Unlock this article and so much more with Hello Interview Premium

Unlock Premium Coding Content

Interactive algorithm visualizations
Reading Progress

On This Page