-
Notifications
You must be signed in to change notification settings - Fork 43
/
word-break-ii.py
296 lines (250 loc) · 9.06 KB
/
word-break-ii.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
140. Word Break II
Hard
Given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in any order.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example 1:
Input: s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"]
Output: ["cats and dog","cat sand dog"]
Example 2:
Input: s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"]
Output: ["pine apple pen apple","pineapple pen apple","pine applepen apple"]
Explanation: Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output: []
Constraints:
1 <= s.length <= 20
1 <= wordDict.length <= 1000
1 <= wordDict[i].length <= 10
s and wordDict[i] consist of only lowercase English letters.
All the strings of wordDict are unique.
"""
# V0
# IDEA : BACKTRCK, LC 078 Subsets
class Solution(object):
def wordBreak(self, s, wordDict):
def help(cur):
"""
NOTE this !!! :
-> shallow copy cur[:]
"""
if "".join(cur[:]) == s:
res.append(" ".join(cur[:]))
return
if len("".join(cur[:])) > len(s):
return
for i in range(len(wordDict)):
cur.append(wordDict[i])
help(cur)
# NOTE this
cur.pop()
# edge case
if not wordDict:
return []
res = []
cur = []
cnt = 0
help(cur)
print ("res = " + str(res))
return res
# V1
# IDEA : RECURSION
# https://leetcode.com/problems/word-break-ii/discuss/1426014/Python-interview-friendly-simple-recursion
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
def recur(s, path):
if not s:
out.append(' '.join(path))
return
for i in range(1,len(s)+1):
w,new_s = s[:i], s[i:]
if w in wordDict:
recur(new_s, path + [w])
wordDict, out = set(wordDict), []
recur(s,[])
return out
# V1
# IDEA : BACKTRCK
# https://leetcode.com/problems/word-break-ii/discuss/44404/Python-backtracking
class Solution:
# @param s, a string
# @param dict, a set of string
# @return a list of strings
def wordBreak(self, s, dic):
if not dic:
return []
n = max(len(d) for d in dic)
stack, parents = [0], collections.defaultdict(set)
while stack:
parent = stack.pop()
for child in range(parent+1, parent+n+1):
if s[parent:child] in dic:
if child not in parents:
stack.append(child)
parents[child].add(parent)
stack, res = [[len(s)]], []
while stack:
r = stack.pop()
if r[0] == 0:
r = [s[i:j] for i, j in zip(r[:-1], r[1:])]
res.append(' '.join(r))
for parent in parents[r[0]]:
stack.append([parent]+r)
return res
# V1
# IDEA : BACKTRACK
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
n = len(s)
result = []
words = []
def match(index):
if index == n:
result.append(" ".join(words))
return
for word in wordDict:
isPrefix = ((index+len(word)) <= n and s[index:(index+len(word))] == word)
if isPrefix:
words.append(word)
match(index + len(word))
words.pop()
match(0)
return result
# V1
# IDEA : DFS
# https://leetcode.com/problems/word-break-ii/discuss/222797/Python-solution
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: List[str]
"""
def dfs(i):
if i == len(s):
return [""]
if i in dic:
return dic[i]
res = []
for j in range(i, len(s)):
head = s[i:j+1]
if head in wordSet:
tmp = dfs(j+1)
for string in tmp:
string = head +" "+string
res.append(string.strip())
dic[i] = res
return res
dic = {}
wordSet = set(wordDict)
return dfs(0)
# V1
# IDEA : Top-Down Dynamic Programming
# https://leetcode.com/problems/word-break-ii/solution/
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
wordSet = set(wordDict)
# table to map a string to its corresponding words break
# {string: [['word1', 'word2'...], ['word3', 'word4', ...]]}
memo = defaultdict(list)
#@lru_cache(maxsize=None) # alternative memoization solution
def _wordBreak_topdown(s):
""" return list of word lists """
if not s:
return [[]] # list of empty list
if s in memo:
# returned the cached solution directly.
return memo[s]
for endIndex in range(1, len(s)+1):
word = s[:endIndex]
if word in wordSet:
# move forwards to break the postfix into words
for subsentence in _wordBreak_topdown(s[endIndex:]):
memo[s].append([word] + subsentence)
return memo[s]
# break the input string into lists of words list
_wordBreak_topdown(s)
# chain up the lists of words into sentences.
return [" ".join(words) for words in memo[s]]
# V1
# IDEA : Bottom-Up Dynamic Programming
# https://leetcode.com/problems/word-break-ii/solution/
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
# quick check on the characters,
# otherwise it would exceed the time limit for certain test cases.
if set(Counter(s).keys()) > set(Counter("".join(wordDict)).keys()):
return []
wordSet = set(wordDict)
dp = [[]] * (len(s)+1)
dp[0] = [""]
for endIndex in range(1, len(s)+1):
sublist = []
# fill up the values in the dp array.
for startIndex in range(0, endIndex):
word = s[startIndex:endIndex]
if word in wordSet:
for subsentence in dp[startIndex]:
sublist.append((subsentence + ' ' + word).strip())
dp[endIndex] = sublist
return dp[len(s)]
# V1
# https://leetcode.com/problems/word-break-ii/solution/
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
# quick check on the characters,
# otherwise it would exceed the time limit for certain test cases.
if set(Counter(s).keys()) > set(Counter("".join(wordDict)).keys()):
return []
wordSet = set(wordDict)
# the dp array stores the positions to insert breaks/stops
dp = [[]] * (len(s)+1)
dp[0] = [[0]]
for endIndex in range(1, len(s)+1):
stops = []
for startIndex in range(0, endIndex):
word = s[startIndex:endIndex]
if word in wordSet:
for subsentence in dp[startIndex]:
copy = subsentence.copy()
copy.append(endIndex)
stops.append(copy)
dp[endIndex] = stops
ret = []
# reconstruct the words list from the positions of breaks/stops
for stops in dp[len(s)]:
words = []
for i in range(len(stops)-1):
words.append(s[stops[i]:stops[i+1]])
ret.append(" ".join(words))
return ret
# V1
# https://leetcode.com/problems/word-break-ii/solution/
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
# quick check on the characters,
# otherwise it would exceed the time limit for certain test cases.
if set(Counter(s).keys()) > set(Counter("".join(wordDict)).keys()):
return []
wordSet = set(wordDict)
dp = [[]] * (len(s)+1)
dp[0] = [[0]]
for endIndex in range(1, len(s)+1):
stops = []
for startIndex in range(0, endIndex):
word = s[startIndex:endIndex]
if word in wordSet:
stops.append([startIndex, endIndex])
dp[endIndex] = stops
ret = []
def wordDFS(sentence, dp_index):
if dp_index == 0:
ret.append(" ".join(sentence))
return
for start, end in dp[dp_index]:
word = s[start:end]
wordDFS([word] + sentence, start)
wordDFS([], len(s))
return ret
# V2