-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path140 Word Break II.py
84 lines (61 loc) · 2.66 KB
/
140 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
"""
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"].
Author: Rajeev Ranjan
"""
from collections import deque
class Solution:
def wordBreak(self, s, dict):
"""
.______ .______ _______ _______ __ ___ ___ _______ _______.
| _ \ | _ \ | ____|| ____|| | \ \ / / | ____| / |
| |_) | | |_) | | |__ | |__ | | \ V / | |__ | (----`
| ___/ | / | __| | __| | | > < | __| \ \
| | | |\ \----.| |____ | | | | / . \ | |____.----) |
| _| | _| `._____||_______||__| |__| /__/ \__\ |_______|_______/
record how to construct the sentences for a given dp
In Word Break, we use a bool array to record whether a dp could be segmented.
Now we should use a vector for every dp to record how to construct that dp from another dp.
Google On Campus Presentation, demonstration questions. 4 Sep 2014, Nanyang Technological University, Singapore
- l e e t c o d e
prefix: d l c
:param s: String
:param dict: a set of string
:return: a list of strings
"""
# dp = [[]] * (len(s) + 1) # namespace reuse
dp = [[] for _ in range(len(s) + 1)]
dp[0].append("dummy")
for i in range(len(s)):
if not dp[i]:
continue
for word in dict:
if s[i:i + len(word)] == word:
dp[i + len(word)].append(word)
# build result
if not dp[-1]:
return []
result = []
self.build_result(dp, len(s), deque(), result)
return result
def build_result(self, dp, cur_index, cur_sentence, result):
"""
dfs recursive
from right to left
"""
# reached, build the result from cur_sentence
if cur_index == 0:
result.append(" ".join(cur_sentence))
return
# dfs
for prefix in dp[cur_index]:
cur_sentence.appendleft(prefix)
self.build_result(dp, cur_index - len(prefix), cur_sentence, result)
cur_sentence.popleft()
if __name__=="__main__":
assert Solution().wordBreak("catsanddog", ["cat", "cats", "and", "sand", "dog"])==['cat sand dog', 'cats and dog']