-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.cpp
More file actions
542 lines (450 loc) · 13.2 KB
/
Trie.cpp
File metadata and controls
542 lines (450 loc) · 13.2 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include <bits/stdc++.h>
using namespace std;
// 208. Implement Trie (Prefix Tree)
/*
Approach:
insert(string word): O(length of word)
We start with first character of the given word and keep current node at root
Then traverse over the word and for each character check if the current
node has a child for that character or not. If not, then make a new node
and add it to its children. And then move to the next node.
Do the same for all characters.
At the end, mark true that a word ends here in the node for last character
search(string word): O(length of word)
Same approach as insert
Traverse the word and move to next node in trie for each character
If the next node does not exist for some character, then return false
At the end, if the word ends here is true then return true else return false
startsWith(string prefix): O(length of prefix)
Same as search
At the end if the whole prefix exists in trie, then return true.
*/
class Node
{
public:
bool endOfWord;
vector<Node *> children;
Node()
{
this->endOfWord = false;
children.assign(26, nullptr);
}
};
class Trie
{
private:
Node *root;
public:
/** Initialize your data structure here. */
Trie()
{
root = new Node();
}
/** Inserts a word into the trie. */
void insert(string word)
{
Node *curr = root;
for (int i = 0; i < word.size(); i++)
{
if (curr->children[word[i] - 'a'] == nullptr)
{
Node *node = new Node();
curr->children[word[i] - 'a'] = node;
}
curr = curr->children[word[i] - 'a'];
}
curr->endOfWord = true;
}
/** Returns if the word is in the trie. */
bool search(string word)
{
Node *curr = root;
for (int i = 0; i < word.size(); i++)
{
if (curr->children[word[i] - 'a'] != nullptr)
curr = curr->children[word[i] - 'a'];
else
return false;
}
return curr->endOfWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix)
{
Node *curr = root;
for (int i = 0; i < prefix.size(); i++)
{
if (curr->children[prefix[i] - 'a'] != nullptr)
curr = curr->children[prefix[i] - 'a'];
else
return false;
}
return true;
}
};
// 211. Design Add and Search Words Data Structure
class WordDictionary
{
private:
Node *root;
public:
/** Initialize your data structure here. */
WordDictionary()
{
this->root = new Node();
}
void addWord(string word)
{
Node *curr = root;
for (int i = 0; i < word.size(); i++)
{
if (curr->children[word[i] - 'a'] == nullptr)
{
Node *node = new Node();
curr->children[word[i] - 'a'] = node;
}
curr = curr->children[word[i] - 'a'];
}
curr->endOfWord = true;
}
bool search(string &word, int idx, Node *curr)
{
if (idx == word.size())
return curr->endOfWord;
if (word[idx] == '.')
{
for (int i = 0; i < curr->children.size(); i++)
{
if (curr->children[i] != nullptr && search(word, idx + 1, curr->children[i]))
return true;
}
}
else if (curr->children[word[idx] - 'a'] != nullptr)
return search(word, idx + 1, curr->children[word[idx] - 'a']);
return false;
}
bool search(string word)
{
return search(word, 0, root);
}
};
// 421. Maximum XOR of Two Numbers in an Array
/*
Approach: O(32*n) = O(n)
Make a Trie using the binary representation of each number
Then we use this trie to find the max XOR for each number
In XOR, opposite bits give 1, and same bits give 0
So, if our current bit is 0, then in trie, we try to go to the next node
with value 1, if available else we go to 0. Similarly for 1 we go to 0.
So, for each number we use the trie to find its max answer. And the
height of trie will be 32 max because int has 32 bits.
Then for each number while traversing we calculate the sum by adding 2^pos to the sum for every XOR = 1
Like for 11010 we have: 2^4 + 2^3 + 0 + 2^1 + 0
So, first to create trie for each number we will need O(32*n).
Since int has only 32 bits, so just go over each bit from leftmost bit, and put it into Trie
And same iteration for finding the max for each number.
For each bit you check if its opposite bit is present as the next child, then add 2*i to the sum
If opposite bit is not present then go to next node, but sum remains the same.
To find the opposite bit we can just use (bit ^ 1) as ^ flips the bit so it will give the opposite bit.
To get the ith bit of a number, we right shift i times, so that ith bit is now rightmost
num >> i, will bring ith bit to rightmost position
now (num >> i) & 1, will give if last bit is 0 or 1, as 0 & 1 = 0, 1 & 1 = 1
Eg: 3, 10, 5, 25, 2, 8
3: 00011
10: 01010
5: 00101
25: 11001
2: 00010
8: 01000
Now using this make a trie. And then using trie we can find the answer for
each number by finding the next favorable bit in trie.
*/
class Node
{
public:
vector<Node *> children; //{0, 1}
Node()
{
this->children.assign(2, nullptr);
}
};
void insert(int num, Node *root)
{
Node *curr = root;
// iterate over all 32 bits i.e. 0 - 31 of the number
for (int i = 31; i >= 0; i--)
{
// to get the ith bit of num
int bit = (num >> i) & 1;
if (curr->children[bit] == nullptr)
{
Node *node = new Node();
curr->children[bit] = node;
}
curr = curr->children[bit];
}
}
int maxXOR(int num, Node *root)
{
int sum = 0;
Node *curr = root;
// iterate over all 32 bits i.e. 0 - 31 of the number
for (int i = 31; i >= 0; i--)
{
// to get the ith bit of num
int bit = (num >> i) & 1;
// if opposite bit is available
if (curr->children[(bit ^ 1)] != nullptr)
{
curr = curr->children[(bit ^ 1)];
sum += (1 << i); // sum = sum + 2^i
}
// else
else
{
curr = curr->children[bit];
}
}
return sum;
}
int findMaximumXOR(vector<int> &nums)
{
Node *root = new Node();
for (int val : nums)
insert(val, root);
int res = 0;
for (int val : nums)
{
int myAns = maxXOR(val, root);
res = max(res, myAns);
}
return res;
}
// 677. Map Sum Pairs
class Node
{
public:
int val;
vector<Node *> children;
Node(int val)
{
this->val = val;
this->children.assign(26, nullptr);
}
};
class MapSum
{
private:
Node *root;
public:
/** Initialize your data structure here. */
MapSum()
{
root = new Node(0);
}
void insert(string key, int val)
{
Node *curr = root;
for (int i = 0; i < key.size(); i++)
{
if (curr->children[key[i] - 'a'] == nullptr)
{
Node *node = new Node(0);
curr->children[key[i] - 'a'] = node;
}
curr = curr->children[key[i] - 'a'];
}
curr->val = val;
}
int getSum(Node *node)
{
int sum = node->val;
for (Node *child : node->children)
{
if (child != nullptr)
sum += getSum(child);
}
return sum;
}
int sum(string prefix)
{
Node *curr = root;
for (int i = 0; i < prefix.size(); i++)
{
if (curr->children[prefix[i] - 'a'] == nullptr)
return 0;
curr = curr->children[prefix[i] - 'a'];
}
return getSum(curr);
}
};
// 472. Concatenated Words
/*
Approach: O(n * maxWordLength^2), Trie + DP
First make a Trie using all words.
Then similar to word break, for each word we iterate over it until it is present in Trie.
If at any character, we find that a word has ended here, then we make a call to check for the remaining
word again from the top of the trie. And after the whole word has ended if the count is 2 or more then return true.
Eg:
Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
Here for "catsdogcats"
We start checking for 0th index. This goes until it finds that 'cat' is a word.
So, then we check for the remaining "sdogcats" from the top of the trie again.
We cannot just keep checking after the 't' node in trie, because then we will find that
'cats' is also a word, and the count will become 2. But althought there are 2 words in 'cats' i.e. cat and cats
but they are not concatenated.
So, we need to check for the next word from the root again.
Also, for eg: [a, aa,aaa,aaaaa,aaaaaa]
In this case we will keep checking for same case again and again
So, use memoization to improve the solution.
*/
class Node
{
public:
bool endOfWord;
vector<Node *> children;
Node()
{
this->endOfWord = false;
children.assign(26, nullptr);
}
};
Node *root;
void insert(string word)
{
Node *curr = root;
for (int i = 0; i < word.size(); i++)
{
if (curr->children[word[i] - 'a'] == nullptr)
{
Node *node = new Node();
curr->children[word[i] - 'a'] = node;
}
curr = curr->children[word[i] - 'a'];
}
curr->endOfWord = true;
}
bool isConcatenated(string &word, int idx, int count, vector<int> &dp)
{
// if word is finished, then if count >= 2, then return true, else false
if (idx == word.size())
return count > 1;
if (dp[idx] != -1)
return dp[idx];
// start checking from root of Trie
Node *curr = root;
for (int i = idx; i < word.size(); i++)
{
// if the next character is not present in trie, then just return false
if (curr->children[word[i] - 'a'] == nullptr)
return dp[idx] = false;
curr = curr->children[word[i] - 'a'];
// if current character is a end of word, then we increase count by 1, and check if the remaining string is concatenated
if (curr->endOfWord)
{
if (isConcatenated(word, i + 1, count + 1, dp))
return dp[idx] = true;
}
}
return dp[idx] = false;
}
vector<string> findAllConcatenatedWordsInADict(vector<string> &words)
{
root = new Node();
// Insert all words into Trie
for (string &word : words)
insert(word);
vector<string> res;
for (string &word : words)
{
// create a new dp for this word
vector<int> dp(word.size(), -1);
// if this word is concatenated, then push it into result
if (isConcatenated(word, 0, 0, dp))
res.push_back(word);
}
return res;
}
// 212. Word Search II
/*
Approach: O((n*m)*(4^(n*m)))
Put all words into Trie. For each node also keep the word that end there.
Then go to each element in the matrix, if the root has a child for that character, then start searching for any
words in the trie starting from that character
For searching check all 4 directions of the current cell, and if the current trie node has a child
with that character, then move to that cell.
For each cell check if the endOfWord for the current node is true, then add the word into result, and mark
the endOfWord as false so that same word is not added more than once.
Also mark each cell visited so that you dont visit again, and unmark while backtracking.
*/
class Node
{
public:
bool endOfWord;
string word;
vector<Node *> children;
Node()
{
this->endOfWord = false;
children.assign(26, nullptr);
}
};
Node *root;
vector<string> res;
void insert(string word)
{
Node *curr = root;
for (int i = 0; i < word.size(); i++)
{
if (curr->children[word[i] - 'a'] == nullptr)
{
Node *node = new Node();
curr->children[word[i] - 'a'] = node;
}
curr = curr->children[word[i] - 'a'];
}
curr->endOfWord = true;
curr->word = word;
}
void search(vector<vector<char>> &board, int i, int j, Node *curr)
{
int n = board.size(), m = board[0].size();
if (curr->endOfWord)
{
res.push_back(curr->word);
curr->endOfWord = false;
}
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
char ch = board[i][j];
board[i][j] = '#';
for (int d = 0; d < 4; d++)
{
int x = i + dir[d][0];
int y = j + dir[d][1];
if (x >= 0 && y >= 0 && x < n && y < m && board[x][y] != '#' && curr->children[board[x][y] - 'a'] != nullptr)
search(board, x, y, curr->children[board[x][y] - 'a']);
}
board[i][j] = ch;
}
vector<string> findWords(vector<vector<char>> &board, vector<string> &words)
{
int n = board.size(), m = board[0].size();
root = new Node();
for (string &word : words)
insert(word);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (root->children[board[i][j] - 'a'] != nullptr)
search(board, i, j, root->children[board[i][j] - 'a']);
}
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}