-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path1408. String Matching in an Array.cpp
242 lines (208 loc) · 6.89 KB
/
1408. String Matching in an Array.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
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
//Runtime: 8 ms, faster than 40.00% of C++ online submissions for String Matching in an Array.
//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for String Matching in an Array.
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
int n = words.size();
set<string> ans;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i != j && words[i].find(words[j]) != string::npos){
ans.insert(words[j]);
}
}
}
return vector<string>(ans.begin(), ans.end());
}
};
class Node{
public:
vector<Node*> children;
Node(){
children = vector<Node*>(26, nullptr);
}
};
class SuffixTree{
public:
Node* root = new Node();
void add(string& word){
Node* cur = root;
for(char c: word){
if(cur->children[c-'a'] == nullptr){
cur->children[c-'a'] = new Node();
}
cur = cur->children[c-'a'];
}
};
bool get(string& word){
Node* cur = root;
for(char c : word){
if(cur->children[c-'a'] == nullptr){
return false;
}
cur = cur->children[c-'a'];
}
return true;
};
};
//Suffix tree
//https://leetcode.com/problems/string-matching-in-an-array/discuss/575147/Clean-Python-3-suffix-trie-O(NlogN-%2B-N-*-S2)
//Time: O(NlogN + N * S^2), where S is the max length of all words.
//NlogN for sorting and N * S^2 for build suffix trie.
//Space: O(N * S^2) for suffix trie
//Runtime: 28 ms, faster than 29.06% of C++ online submissions for String Matching in an Array.
//Memory Usage: 29.1 MB, less than 100.00% of C++ online submissions for String Matching in an Array.
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
SuffixTree* st = new SuffixTree();
vector<string> ans;
//sort by their length, descending
//because the longer word cannot be a suffix of a shorter word
sort(words.begin(), words.end(), [](string& a, string& b){
return a.size() > b.size();
});
for(string& word : words){
// cout << word << endl;
if(st->get(word)){
ans.push_back(word);
}
//add suffix of word into the suffix tree
for(int i = 0; i < word.size(); i++){
string suffix = word.substr(i, word.size()-i);
st->add(suffix);
}
}
return ans;
}
};
//counting on trie nodes in suffix tree
//https://leetcode.com/problems/string-matching-in-an-array/discuss/575147/Clean-Python-3-suffix-trie-O(NlogN-%2B-N-*-S2)
//Runtime: 36 ms, faster than 23.11% of C++ online submissions for String Matching in an Array.
//Memory Usage: 29 MB, less than 100.00% of C++ online submissions for String Matching in an Array.
//time: O(N * S^2), space: O(N * S^2)
class Node{
public:
vector<Node*> children;
int count;
Node(){
children = vector<Node*>(26, nullptr);
count = 0;
}
};
class SuffixTree{
public:
Node* root = new Node();
void add(string& word){
Node* cur = root;
for(char c: word){
if(cur->children[c-'a'] == nullptr){
cur->children[c-'a'] = new Node();
}
cur = cur->children[c-'a'];
//first go to that child and then increase its visit count
cur->count += 1;
// cout << c << " " << cur->count << " | ";
}
// cout << endl;
};
bool get(string& word){
Node* cur = root;
for(char c : word){
if(cur->children[c-'a'] == nullptr){
return false;
}
cur = cur->children[c-'a'];
}
// cout << word << " " << cur->count << endl;
//if cur has been visited more than once, it's a suffix of others
return cur->count > 1;
};
};
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
SuffixTree* st = new SuffixTree();
vector<string> ans;
for(string& word : words){
//add suffix of word into the suffix tree
for(int i = 0; i < word.size(); i++){
string suffix = word.substr(i, word.size()-i);
st->add(suffix);
}
}
for(string& word : words){
if(st->get(word)){
ans.push_back(word);
}
}
return ans;
}
};
//KMP
//https://leetcode.com/problems/string-matching-in-an-array/discuss/576070/C%2B%2B-concise-KMP-solution-beats-20!
//Runtime: 24 ms, faster than 32.68% of C++ online submissions for String Matching in an Array.
//Memory Usage: 10.8 MB, less than 100.00% of C++ online submissions for String Matching in an Array.
class Solution {
public:
vector<int> build(string& pat){
int n = pat.size();
vector<int> lps(n, 0);
for(int i = 1, len = 0; i < n; ){
if(pat[i] == pat[len]){
len++;
lps[i] = len;
i++;
}else if(len > 0){
//fallback to compare with a shorter prefix/suffix
len = lps[len-1];
}else{
//len equals to 0 here
lps[i] = 0;
i++;
}
}
return lps;
};
bool search(string& text, string& pat){
int m = text.size(), n = pat.size();
if(n == 0) return false;
vector<int> lps = build(pat);
for(int i = 0, j = 0; i < m; ){
if(text[i] == pat[j]){
i++;
j++;
}
if(j == n){
return true;
}
if(i < m && text[i] != pat[j]){
if(j > 0){
//j now serves as len in bulid()
j = lps[j-1];
}else{
i++;
}
}
}
return false;
};
vector<string> stringMatching(vector<string>& words) {
vector<string> ans;
//sort by length, ascending
sort(words.begin(), words.end(), [](string& a, string& b){
return a.size() < b.size();
});
for(int pi = 0; pi < words.size(); pi++){
//examine pattern one by one
for(int ti = pi+1; ti < words.size(); ti++){
if(search(words[ti], words[pi])){
ans.push_back(words[pi]);
//if words[pi] is a substring of any longer words, break at once so that there won't be duplicate element in ans
break;
}
}
}
return ans;
}
};