-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.cpp
More file actions
285 lines (243 loc) · 6.67 KB
/
Copy pathTrie.cpp
File metadata and controls
285 lines (243 loc) · 6.67 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// #ifndef GFG
// #define GFG
// #endif
/*
TRIE
Efficient for the following operations on words in a dictionary.
- Search, insert, delete, Prefix Search, Lexicographically ordering of words
*/
namespace Trie{
struct TrieNode
{
public:
bool isEnd;
vector<TrieNode *> children;
TrieNode(bool isEnd = false)
{
this->isEnd = isEnd;
children.resize(26, nullptr);
}
};
class Trie
{
public:
TrieNode *root;
Trie() : root{new TrieNode()} {}
~Trie(){
freeNode(root);
}
void freeNode(TrieNode* cur){
for(int i=0; i<cur->children.size(); ++i){
if(cur->children[i]!=nullptr){
freeNode(cur->children[i]);
}
}
delete cur;
}
void insert(const string &word)
{
TrieNode *cur = root;
for (int i = 0; i < word.size(); ++i)
{
if (cur->children[word[i] - 'a'] == nullptr)
{
cur->children[word[i] - 'a'] = new TrieNode();
}
cur = cur->children[word[i] - 'a'];
}
cur->isEnd = true;
}
bool search(const string &word)
{
TrieNode *cur = root;
for (int i = 0; i < word.size(); ++i)
{
if (cur->children[word[i] - 'a'] == nullptr)
{
return false;
}
cur = cur->children[word[i] - 'a'];
}
return cur->isEnd;
}
void remove(const string &word)
{
if (search(word))
recurRemove(root, word, 0);
}
bool recurRemove(TrieNode *cur, const string &word, int i)
{
bool hasChild = true;
if (i != word.size())
{
hasChild = recurRemove(cur->children[word[i] - 'a'], word, i + 1);
}
else
{
cur->isEnd = false;
}
if (!hasChild)
{
delete cur->children[word[i] - 'a'];
cur->children[word[i] - 'a'] = nullptr;
}
for (int j = 0; j < cur->children.size(); ++j)
{
if (cur->children[j] != nullptr)
{
return true;
}
}
return cur->isEnd;
}
#ifdef GFG
TrieNode *recurRemove(TrieNode *cur, const string &str, int i)
{
if (cur == nullptr)
return nullptr;
if (i == str.size())
{
cur->isEnd = false;
bool hasNoChild = true;
for (int j = 0; j < cur->children.size(); ++j)
{
if (cur->children[j] != nullptr)
{
hasNoChild = false;
break;
}
}
if (hasNoChild)
{
delete cur;
cur = nullptr;
}
return root;
}
cur->children[word[i] - 'a'] = recurRemove(cur->children[word[i] - 'a'], str, i + 1);
bool hasNoChild = true;
for (int j = 0; j < cur->children.size(); ++j)
{
if (cur->children[j] != nullptr)
{
hasNoChild = false;
break;
}
}
if (hasNoChild && cur->isEnd == true)
{
delete cur;
cur = nullptr;
}
return cur;
}
#endif
void printAllData()
{
string curStr = "";
recurPrint(root, curStr);
}
void recurPrint(TrieNode *cur, string &curStr)
{
if (cur == nullptr)
{
return;
}
else if (cur->isEnd)
{
cout << curStr << endl;
}
for (int i = 0; i < cur->children.size(); ++i)
{
curStr += 'a' + i;
recurPrint(cur->children[i], curStr);
curStr.pop_back();
}
}
};
void testTrie()
{
Trie trie;
vector<string> words{"an", "and", "ant", "bad", "bat", "zoo"};
for (const string &word : words)
{
trie.insert(word);
}
trie.printAllData();
cout << endl;
cout << boolalpha << trie.search("bat") << endl;
cout << boolalpha << trie.search("bet") << endl;
cout << endl;
trie.remove("and");
trie.remove("an");
trie.remove("zoo");
trie.printAllData();
cout << endl;
}
}
namespace CountDistinctRowsInBinaryMatrix{
struct TrieNode{
bool isEnd;
vector<TrieNode*> children;
TrieNode(bool isEnd=false):isEnd{isEnd}{
children.resize(2,nullptr);
}
};
struct BinaryTrie{
TrieNode* root;
BinaryTrie():root{new TrieNode()}{}
~BinaryTrie(){
freeNode(root);
}
void freeNode(TrieNode* cur){
for(int i=0; i<cur->children.size(); ++i){
if(cur->children[i]!=nullptr){
freeNode(cur->children[i]);
}
}
delete cur;
}
bool insert(const vector<int>& binaryRow){
TrieNode* cur=root;
for(int i=0; i<binaryRow.size(); ++i){
if(cur->children[binaryRow[i]]==nullptr){
cur->children[binaryRow[i]]=new TrieNode();
}
cur=cur->children[binaryRow[i]];
}
// already exist
if(cur->isEnd){
return false;
}
cur->isEnd=true;
return true;
}
};
// T/C: O(m*n). m==rows, n==cols
// S/C: O(m*n)
int getDistinctRows(const vector<vector<int>>& binaryMat){
BinaryTrie trie;
int nDistinct=0;
for(const vector<int>& row : binaryMat){
if(trie.insert(row)){
nDistinct++;
}
}
return nDistinct;
}
}
int main(){
//Trie::testTrie();
vector<vector<int>> binaryMat={
{1,0,0},
{1,1,1},
{1,0,0},
{0,1,0}
};
cout << CountDistinctRowsInBinaryMatrix::getDistinctRows(binaryMat) << endl;
return 0;
}