2Brute2Best
🔥0

Partition Labels

Medium·LeetCode #763 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You get a string s of lowercase letters. Cut it into consecutive parts so that each letter appears in only one part.

Make as many parts as you can. Return the length of each part, from left to right.

Input

A string s of lowercase English letters.

Output

A list of part lengths, in order. The lengths add up to s.length.

Example 1
Input: s = "ababcbacadefegdehijhklij"
Output: [9, 7, 8]

The parts are ababcbaca, defegde and hijhklij. No letter is in two parts. Merging the last two parts would give fewer parts, so that is not the best answer.

Example 2
Input: s = "eccbbbbdec"
Output: [10]

The letters e and c show up on both sides of every possible cut, so the only option is one part.

Constraints
  • • 1 ≤ s.length ≤ 500
  • • s consists of lowercase English letters
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“each letter appears in at most one part”
→ A letter can't be cut in two. Its span, from first to last occurrence, has to stay inside one part.
“as many parts as possible”
→ Maximise the number of cuts: cut as often as is safe.
“size of these parts, in order”
→ Output the lengths of consecutive blocks, left to right.
03

Approach Complexity Ladder

Brute Force — test every cut positionO(N²)O(1)

For each cut, check whether any letter appears on both sides

Optimal — cut as soon as the reach closesO(N)O(1)

Record each letter's last index; cut when the index reaches the furthest last index seen in this part

Brute Force Intuition

A cut after index i is allowed only if no letter appears both at or before i and after i. Test every position, cut at each allowed one, and return the part lengths.

Recognition rule for this sub-pattern:

To fit the most activities, always take the one that finishes earliest.
Remember this · Greedy
Trigger
Maximise count / minimise cost with a natural "best next step", sorting makes the choice obvious, reachability.
Think
Is there a choice I can always make safely without looking ahead?
Key question
Why can't the greedy choice be worse than the optimal one? (Try to break it with a small example.)
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.