Word Break
Problem
You are given a string s and a list of words wordDict. Decide whether s can be split into a sequence of one or more pieces, each of which appears in wordDict. Words from the dictionary may be reused any number of times, and every character of s must belong to exactly one piece. Return True or False.
sis a lowercase string up to a few hundred characters long.wordDictholds up to a thousand distinct lowercase words, each up to about twenty characters.
Examples
s = "bluesky", wordDict = ["blue", "sky"]→True, split asblue | sky.s = "lampshadex", wordDict = ["lamp", "shade", "lam", "pshade"]→False. Every split leaves a strandedx.s = "ccccd", wordDict = ["c", "cc"]→False. Thedis never covered no matter how thecs are grouped.
Hints
- Suppose the string can be segmented. Consider the last word in the segmentation. What must be true of the prefix that comes before it?
- Recursion that tries every possible last word recomputes the same prefixes many times. What is the natural thing to remember?
- A boolean DP over prefix lengths:
ok[i]is true if somej < ihasok[j]true ands[j:i]in the dictionary.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.