Word Break
Question (LC.139)
Example
I: "helloworld", ["hello", "world"]
O: true
I: "hellow", ["hello", "world"]
O: falseBrute Force Search
Single Sequence DP
1. Define optimal subproblem
BK[n] = is [0:n] a separatable string
2. Solve by prefix
for i from 0 to n-1
if BK[i] and dict.contains(S[i:n])
BK[n] = true
otherwise by default BK[n] = false
remember array index is 1 behind dp index
3. Base case
BK[0] = true
4. Topo order
for i from 1 to n
5. Final answer
return BK[n]Time & Space Complexity
Follow Up (LC.140)
Example
Memoized Search
Code
Time Complexity
Last updated