Word Break
Decide whether a string can be segmented into dictionary words.
Why does this pattern fit?
Restate the exact job
Decide whether a string can be segmented into dictionary words.
A prefix is solvable if some earlier solvable cut is followed by a dictionary word.
O(n²) time · O(n + dictionary) space
Greedily taking the longest matching word can trap a valid segmentation.
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.