跳到主要内容

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"].

分析​

在上一题的基础上,要返回解本身。

代码​

# Word Break II
# 动规,时间复杂度O(n^2),空间复杂度O(n^2)
class Solution:
def wordBreak(self, s: str, wordDict: set[str]) -> list[str]:
# 长度为n的字符串有n+1个隔板
f = [False] * (len(s) + 1)
# prev[i][j]为true,表示s[j, i)是一个合法单词,可以从j处切开
# 第一行未用
prev = [[False] * len(s) for _ in range(len(s) + 1)]
f[0] = True
for i in range(1, len(s) + 1):
for j in range(i - 1, -1, -1):
if f[j] and s[j:i] in wordDict:
f[i] = True
prev[i][j] = True

result = []
path = []
self.gen_path(s, prev, len(s), path, result)
return result

# DFS遍历树,生成路径
def gen_path(self, s: str, prev: list[list[bool]], cur: int,
path: list[str], result: list[str]) -> None:
if cur == 0:
result.append(' '.join(reversed(path)))
return

for i in range(len(s)):
if prev[cur][i]:
path.append(s[i:cur])
self.gen_path(s, prev, i, path, result)
path.pop()

相关题目​