Word Break
State: f[i]表示前i个位置/数字/字符,第i个...
一般answer是f(n)而不是f(n-1)
139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
BackTracking
Brute Force: 2^n => see explain
TC: (length of s) * (size of wordDict)
SC: n, depth of str
DP
State: f[i] 表示前i個字被完美切分
function: f[i] = OR{f[j]}, j<i && j+1 to i is a word
Init: f[0] = true
Ans: f[n]
140 Word Break II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.
Backtracking
O: (length of s) * (size of wordDict) * (number of answer)
Last updated
Was this helpful?