140. Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example,

 given*s* = "catsanddog",
 *dict* = ["cat", "cats", "and", "sand", "dog"].
 A solution is ["cats and dog", "cat sand dog"].

第一步建立字符串中分詞的狀態,設 f[i] 為 boolean 狀態表示以 s[0,i]能被
dict 分割,狀態方程:
f?[i?][j] = true if s[i][j] ∈ ???dict

c a t s a n d d o g
c F F T T F F F F F F
a F F F F F F F F F
t F F F F F F F F
s F F F T F F F
a F F T F F F
n F F F F F
d F F F F
d F F T
o F F
g F

第二部通過 DFS 恢復成句子。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容