-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path140. Word Break II.cpp
175 lines (151 loc) · 5.08 KB
/
140. Word Break II.cpp
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
//DP
//TLE
//31 / 36 test cases passed.
class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
int n = s.size();
if(n == 0) return vector<string>();
vector<vector<string>> segs(n);
for(int i = n-1; i >= 0; --i){
//check if s[i:] is itself a valid dict word
if(find(wordDict.begin(), wordDict.end(), s.substr(i)) != wordDict.end()){
segs[i].push_back(s.substr(i));
}
//split s[i] into two parts
/*
s[i:] : s[i:j-1] + s[j:]
s[i:j-1] is a valid dictionary word
s[j:] is composed by valid dictionary words
*/
for(int j = n-1; j >= i+1; --j){
// cout << "j: " << j << ", segs[j].size(): " << segs[j].size() << endl;
// cout << "finding word: " << s.substr(i, j-i) << endl;
if(!segs[j].empty() &&
find(wordDict.begin(), wordDict.end(), s.substr(i, j-i)) != wordDict.end()){
for(string& prev_seg : segs[j]){
// cout << s.substr(i, j-i) << " + " << prev_seg << endl;
segs[i].push_back(s.substr(i, j-i) + " " + prev_seg);
}
}
}
}
// for(int i = 0; i < n; ++i){
// cout << "substr: " << s.substr(i) << endl;
// for(string& seg : segs[i]){
// cout << seg << endl;
// }
// }
return segs[0];
}
};
//DP + trie
//TLE
//31 / 36 test cases passed.
class TrieNode {
public:
vector<TrieNode*> children;
bool end;
TrieNode(){
children = vector<TrieNode*>(26, nullptr);
end = false;
}
};
class Trie {
public:
TrieNode* root;
Trie(){
root = new TrieNode();
}
void add(string& word){
TrieNode* cur = root;
for(char c : word){
if(!cur->children[c-'a']){
cur->children[c-'a'] = new TrieNode();
}
cur = cur->children[c-'a'];
}
cur->end = true;
}
bool find(string word){
TrieNode* cur = root;
for(char c : word){
if(!cur->children[c-'a']){
return false;
}
cur = cur->children[c-'a'];
}
return cur->end;
}
};
class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
int n = s.size();
if(n == 0) return vector<string>();
Trie* trie = new Trie();
for(string& word : wordDict){
trie->add(word);
}
vector<vector<string>> segs(n);
for(int i = n-1; i >= 0; --i){
//check if s[i:] is itself a valid dict word
if(trie->find(s.substr(i))){
segs[i].push_back(s.substr(i));
}
//split s[i] into two parts
/*
s[i:] : s[i:j-1] + s[j:]
s[i:j-1] is a valid dictionary word
s[j:] is composed by valid dictionary words
*/
for(int j = n-1; j >= i+1; --j){
// cout << "j: " << j << ", segs[j].size(): " << segs[j].size() << endl;
// cout << "finding word: " << s.substr(i, j-i) << endl;
if(!segs[j].empty() && trie->find(s.substr(i, j-i))){
for(string& prev_seg : segs[j]){
// cout << s.substr(i, j-i) << " + " << prev_seg << endl;
segs[i].push_back(s.substr(i, j-i) + " " + prev_seg);
}
}
}
}
// for(int i = 0; i < n; ++i){
// cout << "substr: " << s.substr(i) << endl;
// for(string& seg : segs[i]){
// cout << seg << endl;
// }
// }
return segs[0];
}
};
//DFS + memorization, each time move a "word" forward
//https://leetcode.com/problems/word-break-ii/discuss/44167/My-concise-JAVA-solution-based-on-memorized-DFS
//Runtime: 12 ms, faster than 93.80% of C++ online submissions for Word Break II.
//Memory Usage: 10.1 MB, less than 84.03% of C++ online submissions for Word Break II.
class Solution {
public:
unordered_map<string, vector<string>> memo;
void dfs(string s, vector<string>& wordDict){
if(memo.find(s) != memo.end()){
return;
}
if(s.size() == 0){
memo[s] = {""};
return;
}
for(string& word : wordDict){
if(s.rfind(word, 0)==0){ //startswith
//split s into word + s.substr(word.size())
dfs(s.substr(word.size()), wordDict);
for(string& seg : memo[s.substr(word.size())]){
memo[s].push_back(word + ((!seg.empty()) ? " " : "") + seg);
}
}
}
};
vector<string> wordBreak(string s, vector<string>& wordDict) {
dfs(s, wordDict);
return memo[s];
}
};