-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path139. Word Break.py
41 lines (37 loc) · 1.02 KB
/
139. Word Break.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
# BFS
queue = []
queue.append(0)
visited = [0] * len(s)
while queue:
st = queue.pop(0)
if visited[st] == 0:
for ed in range(st, len(s)):
if s[st:ed + 1] in wordDict:
queue.append(ed + 1)
if ed == len(s) - 1:
return True
visited[st] = 1
return False
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
# dp
dp = [False] * (len(s) + 1)
dp[0] = True
for i in range(len(s) + 1):
for j in range(0, i):
if dp[j] and s[j:i] in wordDict:
dp[i] = True
break
return dp[-1]