# [Leetcode][python]Word Break/Word Break II/单词拆分/单词拆分 II

146 篇文章 4 订阅
145 篇文章 13 订阅

## Word Break

### 代码

class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
n = len(s)
dp = [False] * (n + 1)
dp[0] = True
for i in range(n):
for j in range(i, -1, -1):
print j, i, s[j:i + 1], dp
if dp[j] and s[j:i + 1] in wordDict:
dp[i + 1] = True
break
print '-----loop-----'
return dp[n]

0 0 l [True, False, False, False, False, False, False, False, False]
-----loop-----
1 1 e [True, False, False, False, False, False, False, False, False]
0 1 le [True, False, False, False, False, False, False, False, False]
-----loop-----
2 2 e [True, False, False, False, False, False, False, False, False]
1 2 ee [True, False, False, False, False, False, False, False, False]
0 2 lee [True, False, False, False, False, False, False, False, False]
-----loop-----
3 3 t [True, False, False, False, False, False, False, False, False]
2 3 et [True, False, False, False, False, False, False, False, False]
1 3 eet [True, False, False, False, False, False, False, False, False]
0 3 leet [True, False, False, False, False, False, False, False, False]
-----loop-----
4 4 c [True, False, False, False, True, False, False, False, False]
3 4 tc [True, False, False, False, True, False, False, False, False]
2 4 etc [True, False, False, False, True, False, False, False, False]
1 4 eetc [True, False, False, False, True, False, False, False, False]
0 4 leetc [True, False, False, False, True, False, False, False, False]
-----loop-----
5 5 o [True, False, False, False, True, False, False, False, False]
4 5 co [True, False, False, False, True, False, False, False, False]
3 5 tco [True, False, False, False, True, False, False, False, False]
2 5 etco [True, False, False, False, True, False, False, False, False]
1 5 eetco [True, False, False, False, True, False, False, False, False]
0 5 leetco [True, False, False, False, True, False, False, False, False]
-----loop-----
6 6 d [True, False, False, False, True, False, False, False, False]
5 6 od [True, False, False, False, True, False, False, False, False]
4 6 cod [True, False, False, False, True, False, False, False, False]
3 6 tcod [True, False, False, False, True, False, False, False, False]
2 6 etcod [True, False, False, False, True, False, False, False, False]
1 6 eetcod [True, False, False, False, True, False, False, False, False]
0 6 leetcod [True, False, False, False, True, False, False, False, False]
-----loop-----
7 7 e [True, False, False, False, True, False, False, False, False]
6 7 de [True, False, False, False, True, False, False, False, False]
5 7 ode [True, False, False, False, True, False, False, False, False]
4 7 code [True, False, False, False, True, False, False, False, False]
-----loop-----

## Word Break II

### 代码

class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: List[str]
"""
Solution.res = []
self.dfs(s, wordDict, '')
return Solution.res

def dfs(self, s, wordDict, stringlist):
if self.check(s, wordDict):
# 如果s已经切完，则加入最后结果集
if len(s) == 0:
Solution.res.append(stringlist[1:])
for i in range(1, len(s)+1):
if s[:i] in wordDict:
print stringlist+' '+s[:i]
self.dfs(s[i:], wordDict, stringlist+' '+s[:i])

def check(self, s, wordDict):
dp = [False for i in range(len(s)+1)]
dp[0] = True
# 这里循环是len(s)，使得该check函数变成了只要有单词在里面就验证成功，和wordbreak有所不同！
for i in range(len(s)):
for j in range(i, -1, -1):
if dp[j] and s[j:i + 1] in wordDict:
dp[i + 1] = True
break
return dp[len(s)]

 cat
cat sand
cat sand dog
cats
cats and
cats and dog

## 总结

• 0
点赞
• 1
收藏
觉得还不错? 一键收藏
• 0
评论
04-14 1万+
06-05 682

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。