#763

Partition Labels

MediumGreedyHas vizHash TableStringGreedyTwo Pointers

Problem

You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.

Note that the partition is done so that after concatenating all parts in order, the resultant string should be s.

Return a list of integers representing the size of these parts.

Example 1

Input: s = "ababcbacadefegdehijhklij"

Output: [9,7,8]

Partition as ababcbaca / defegde / hijhklij.

Example 2

Input: s = "eccbbbbdec"

Output: [10]

All letters share one partition.

Constraints

  • 1 <= s.length <= 500
  • s consists of lowercase English letters.
Loading visualization…

Explanation

Partition Labels is a Medium interview problem tagged [Hash Table, String, Greedy, Two Pointers]. The winning approach is typically **Greedy**.

Intuition

When an exchange argument holds, greedy beats heavier DP.

Approach

Sort by a key, then make the locally best choice that preserves a global invariant.

  1. Propose a sort key.
  2. Scan once maintaining state.
  3. Prove no better swap exists—or test counterexamples.

Solution

Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.

Pattern templateGreedy

Edge cases

  • •Empty or single-element input
  • •Duplicates and ties
  • •Maximum constraints / overflow
  • •Disconnected or unreachable states (for graphs)

Complexity

Time: O(n log n) or O(n)

Space: O(n)

Before coding, restate the problem, name the pattern (Greedy), and write the invariant or DP state in one sentence.

Learn the topic

Got it?

Mark #763 Partition Labels as understood when the pattern clicks.