-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
77 lines (55 loc) · 1.57 KB
/
Solution.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
dp[i]: s[:i] is work break ?
dp[0] = True
dp[j] = dp[i] if A[i:j] in memo
= False else
01234567
leetcode
0 1 2 3 4 5 6 7 8
dp T F F F T T
j
1,2,3
4 T
5,6,7
8 T
ex:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
"""
def wordBreak139(self, s: str, wordDict: List[str]) -> bool:
memo = set(wordDict)
dp = [False for _ in range(len(s) + 1)]
dp[0] = True
for j in range(len(dp)):
for i in range(j):
tmp = s[i:j]
if tmp in memo and dp[i]:
dp[j] = True
return dp[-1]
"""
dp[i]: A[:i] is word break able
dp[0] = True
dp[j] = dp[i] if A[i:j] in memo
0 1 2 3 4 5 6 7 8 9
c a t s a n d d o g
dp T F F [""]
T ["cat"]
T F F ["cats"]
T F F ["cat sand", "cats and"]
T ["cat sand dog", "cat sand dog"]
"""
def wordBreak140(s, wordDict)
memo = set(wordDict)
dp = [False for _ in range(len(s) + 1)]
dp[0] = True
strs = {i : [] for i in range(len(s) + 1)}
strs[0] = [""]
for j in range(len(dp)):
for i in range(j):
tmp = s[i:j]
if dp[i] and tmp in memo:
dp[j] = True
for prefix in strs[i]:
strs[j].append((prefix + " " if prefix != "" else "") + tmp)
# print("prefix: {}, i:{} j: {}, tmp:{}, str[j]: {}".format(prefix, i, j, tmp, strs[j]))
return [x for x in strs[len(dp) - 1]]