LeetCode 139 · Medium

Word Break Explained (LeetCode 139)

Updated Nov 27, 2026 · 3 min read

Keep a list ok where ok[i] is True when the first i letters can be split into dictionary words. ok[0] is True, and ok[i] is True if some earlier j has ok[j] True and s[j:i] is a word. The answer is ok[n], found with about n² checks instead of trying all 2^(n-1) splits.

Word Break reel cover

Key points

  • ●Trying every split is 2^(n-1): 2,048 tries for 12 letters.
  • ●ok[i] means the first i letters can be split into words; ok[0] is True.
  • ●ok[i] is True when some ok[j] is True and s[j:i] is in the word set.
  • ●About n² checks: 78 for 12 letters, each with a substring lookup.
  • ●Put the words in a set so each lookup is fast.
Step-by-step visual sheet: Word Break
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: Ravi's sign lost its spaces

Ravi's shop sign reads icecreamcone, 12 letters with no spaces. His word list is ice, cream, icecream, cone and on. Can the sign still be read as a run of those words, with every letter used?

Yes: "ice cream cone", or "icecream cone". That's LeetCode 139, Word Break: given a string and a list of words, decide whether the whole string can be cut into words from the list. A word can be used as many times as you like.

The slow way: try every split

Between 12 letters there are 11 gaps, and each gap either gets a space or doesn't. That's 2 to the 11, or 2,048 ways to split the sign. Trying them all means rebuilding the same beginnings over and over: "ice" followed by anything gets checked again in every split that starts with "ice". For a 300-letter string, the problem's limit, this never finishes.

The idea: mark every spot where a word can end

Ravi reads it the human way. He doesn't care how he got to a position, only whether he can get there. So he marks positions:

  • Position 0, the start, is marked.
  • From any marked position, if a word starts there, mark the position where it ends.
  • If the end of the string gets marked, the answer is yes.

In code that's a list of booleans:

  • ok[i] means the first i letters can be split into words.
  • ok[0] = True.
  • ok[i] = True if some j < i has ok[j] True and s[j:i] is a word.

Trace it on Ravi's sign

Mark Why
ok[0] the empty start
ok[3] s[0:3] = "ice" from 0
ok[8] s[3:8] = "cream" from 3, or s[0:8] = "icecream" from 0
ok[12] s[8:12] = "cone" from 8

Those four are the only True cells: ok[0], ok[3], ok[8] and ok[12]. ok[12] is the end of the string, so the answer is True. The visual sheet above walks the same table step by step.

Notice that "on" never helps. It does sit inside the sign, at s[9:11], but it could only count if ok[9] were True, and no word ends at 9. A word only matters when it starts on a marked spot.

The code in Python

def word_break(s, word_list):
    words = set(word_list)
    ok = [True] + [False] * len(s)
    for i in range(1, len(s) + 1):
        for j in range(i):
            if ok[j] and s[j:i] in words:
                ok[i] = True
                break
    return ok[len(s)]

For each end i, it tries every start j. The break stops as soon as one good start is found, since one way in is enough. On "icecreamcone" it returns True. On "catsandog" with cats, dog, sand, and, cat, it returns False: you can reach "cats and" or "cat sand" at position 7, but "og" isn't a word, so the end is never marked.

Complexity

  • Time: about n² start and end pairs, 78 for 12 letters, each with a substring lookup in a set.
  • Space: O(n) for the ok list, plus the word set.

From 2,048 splits down to 78 checks on Ravi's sign, and the gap grows fast with longer strings.

The trap to watch for

Two common slips. First, checking words against the list instead of a set makes each lookup slow; build the set once. Second, forgetting that ok has len(s) + 1 cells. The index means "letters so far", so ok[len(s)] is the answer, not ok[len(s) - 1]. If you mix up letter positions and prefix lengths, you'll mark the wrong spots and miss the last word.

Frequently asked questions

Why is ok[0] True in Word Break?

ok[0] stands for the empty prefix, which needs no words at all. It is the starting point: every first word, like 'ice' in 'icecreamcone', begins at position 0 and needs ok[0] to be True to count.

What is the time complexity of Word Break?

There are about n² pairs of start j and end i, and each one does a substring check, so it is O(n²) checks times the cost of slicing and hashing a substring. Space is O(n) for the ok list plus the word set.

Can a word be used more than once?

Yes. The problem allows reusing a word, and the DP handles it naturally, because ok[i] only asks whether some word ends at i, not which words were already used.

Is Word Break the same as Coin Change?

They share a shape. In Coin Change you ask which amounts you can reach with coins; in Word Break you ask which string positions you can reach with words. Both fill a table from small to big, reusing earlier answers.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free