MediumDynamic Programming · Strings

Word Break

Return true if s can be split into a sequence of words from wordDict. Words can be reused any number of times.

"neetcode", ["neet","code"] → true"applepenapple", ["apple","pen","ape"] → true"catsincars", ["cats","cat","sin","in","car"] → false

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

The string

Circles mark split points i: T if s[i:] can be split

Time and space complexity

n = len(s), m = number of words, t = longest word. The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. Lowercase letters; s up to 12 letters for plain recursion and 16 for the others, up to 8 words.