-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path336. Palindrome Pairs.cpp
292 lines (250 loc) · 8.94 KB
/
336. Palindrome Pairs.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
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
//trie
//Runtime: 1144 ms, faster than 11.07% of C++ online submissions for Palindrome Pairs.
//Memory Usage: 280.2 MB, less than 7.29% of C++ online submissions for Palindrome Pairs.
class TrieNode{
public:
vector<TrieNode*> children;
int index;
string word;
TrieNode(){
children = vector<TrieNode*>(26);
word = "";
index = -1;
}
};
class Trie{
public:
TrieNode* root;
Trie(){
root = new TrieNode();
}
void add(string& word, int index){
TrieNode* cur = root;
for(char c : word){
if(cur->children[c-'a'] == nullptr){
cur->children[c-'a'] = new TrieNode();
}
cur = cur->children[c-'a'];
}
cur->word = word;
cur->index = index;
}
vector<TrieNode*> find(string& word){
//this function won't work for "word" which is empty
TrieNode* cur = root;
/*
if we want to find "sll",
both the trie node "s" and "sll" match
*/
vector<TrieNode*> cands;
if(cur->index != -1 && cur->word != word){
//find for empty string
cands.push_back(cur);
}
for(int i = word.size()-1; i >= 0; --i){
char c = word[i];
if(cur->children[c-'a'] == nullptr){
/*
do not return {}!
because we may have found a leaf node previously!
*/
// cout << "end finding" << endl;
return cands;
}
cur = cur->children[c-'a'];
// if((i == 0) && !(cur->index != -1 && cur->word != word)){
if(i == 0){
//reach the end of "word"
//put all leaves of "cur" into cands
//testcase: word: "a", and it want to find "ab"
stack<TrieNode*> stk;
stk.push(cur);
while(!stk.empty()){
cur = stk.top(); stk.pop();
if(cur->index != -1 && cur->word != word){
cands.push_back(cur);
}
for(TrieNode* child : cur->children){
if(child){
stk.push(child);
}
}
}
}else if(cur->index != -1 && cur->word != word){
//cur->index != -1: current node serves as a leaf
//cur->word != word: avoid finding itself!
// cout << "find " << cur->word << endl;
cands.push_back(cur);
}
}
return cands;
}
};
class Solution {
public:
bool isPalindrome(string str){
int n = str.size();
for(int i = 0; i < n-1-i; ++i){
if(str[i] != str[n-1-i]){
return false;
}
}
return true;
};
vector<vector<int>> palindromePairs(vector<string>& words) {
Trie* trie = new Trie();
int n = words.size();
for(int i = 0; i < n; ++i){
trie->add(words[i], i);
}
vector<vector<int>> ans;
for(int i = 0; i < n; ++i){
// cout << "===" << words[i] << "===" << endl;
if(words[i] == ""){
for(int j = 0; j < n; ++j){
if(j == i) continue;
if(isPalindrome(words[j])){
// cout << "push: " << words[j] << endl;
ans.push_back({j, i});
}
}
}else{
vector<TrieNode*> cands = trie->find(words[i]);
if(cands.empty()) continue;
for(TrieNode* cand : cands){
int remainSize = words[i].size() - cand->word.size();
// cout << "cand: " << cand->word << ", remainSize: " << remainSize << endl;
if(remainSize == 0){
ans.push_back({cand->index, i});
}else if(remainSize > 0){
// cout << "check " << words[i].substr(0, remainSize) << " is palindrome? " << isPalindrome(words[i].substr(0, remainSize)) << endl;
if(isPalindrome(words[i].substr(0, remainSize))){
ans.push_back({cand->index, i});
}
}else{
//remainSize < 0
remainSize *= -1;
// cout << "check " << cand->word.substr(cand->word.size()-remainSize, remainSize) << " is palindrome? " << isPalindrome(cand->word.substr(cand->word.size()-remainSize, remainSize)) << endl;
if(isPalindrome(cand->word.substr(cand->word.size()-remainSize, remainSize))){
ans.push_back({cand->index, i});
}
}
}
// cout << endl;
}
}
return ans;
}
};
//trie, each node with a "palins" list
//https://leetcode.com/problems/palindrome-pairs/discuss/79195/O(n-*-k2)-java-solution-with-Trie-structure
//http://www.allenlipeng47.com/blog/index.php/2016/03/15/palindrome-pairs/
//Runtime: 380 ms, faster than 74.84% of C++ online submissions for Palindrome Pairs.
//Memory Usage: 269.4 MB, less than 10.56% of C++ online submissions for Palindrome Pairs.
//time: O(n*k^2) for both building an searching
class TrieNode{
public:
vector<TrieNode*> children;
int index;
vector<int> palins;
TrieNode(){
children = vector<TrieNode*>(26);
index = -1;
}
};
class Trie{
private:
TrieNode* root;
bool isPalindrome(string& str, int l, int r){
// cout << "check ";
// for(int i = l; i <= r; ++i){
// cout << str[i];
// }
// cout << endl;
for(; l < r; ++l, --r){
if(str[l] != str[r]){
return false;
}
}
return true;
};
public:
Trie(){
root = new TrieNode();
}
void add(string& word, int index){
TrieNode* cur = root;
/*
insert into trie in reverse order,
then in "find",
we can search each char in positive order
*/
for(int i = word.size()-1; i >= 0; --i){
/*
note: "cur" in now on the word[i+1]!
so we only have seen [i+1,n-1]!
*/
// cout << "we are at " << word.substr(i+1) << endl;
if(isPalindrome(word, 0, i)){
/*
palins serves as a summary of a node's descendants
*/
/*
we have seen word[i, n-1],
remaining word[0, i-1] needed to be check
*/
cur->palins.push_back(index);
}
char c = word[i];
if(cur->children[c-'a'] == nullptr){
cur->children[c-'a'] = new TrieNode();
}
cur = cur->children[c-'a'];
}
cur->index = index;
//empty string is also a palindrome?
cur->palins.push_back(index);
}
void find(string& word, int index, vector<vector<int>>& res){
TrieNode* cur = root;
for(int i = 0; i < word.size(); ++i){
/*
note: "cur" in now on the word[i-1]!
so we only have seen [0,i-1]!
*/
// cout << "we are at " << word.substr(0, i) << endl;
if(cur->index != -1 && cur->index != index && isPalindrome(word, i, word.size()-1)){
//cur->index != -1: current node serves as a leaf
//cur->index != index: avoid finding itself!
//isPalindrome(word, i+1, word.size()-1): word[i+1,n-1] is a palindrome
res.push_back({index, cur->index});
}
char c = word[i];
if(cur->children[c-'a'] == nullptr) return;
cur = cur->children[c-'a'];
}
//now we have finished the "word" to be searched
//but we haven't go to the leaf
for(int palinIndex : cur->palins){
if(palinIndex == index) continue;
res.push_back({index, palinIndex});
}
}
};
class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
Trie* trie = new Trie();
int n = words.size();
for(int i = 0; i < n; ++i){
// cout << "===add " << words[i] << "===" << endl;
trie->add(words[i], i);
}
vector<vector<int>> res;
for(int i = 0; i < n; ++i){
// cout << "===find " << words[i] << "===" << endl;
trie->find(words[i], i, res);
}
return res;
}
};