PROBLEM 55 OF 75

Word Break

Decide whether a string can be segmented into dictionary words.

PATTERNDynamic programmingState is the smallest information needed to finish.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Decide whether a string can be segmented into dictionary words.

WHY THIS FITS

A prefix is solvable if some earlier solvable cut is followed by a dictionary word.

COMPLEXITY

O(n²) time · O(n + dictionary) space

COMMON FAILURE

Greedily taking the longest matching word can trap a valid segmentation.

THE REASONING, IN ONE PLACE

How to solve Word Break

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Decide whether a string can be segmented into dictionary words.

Why Dynamic programming fits

A prefix is solvable if some earlier solvable cut is followed by a dictionary word.

State to maintain

Boolean dp over prefix lengths.

Transition

For each end position, test valid earlier cuts and mark the prefix once one works.

Time and space

O(n²) time · O(n + dictionary) space

Counterexample to the tempting mistake

Greedily taking the longest matching word can trap a valid segmentation.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗