-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path208. Implement Trie (Prefix Tree).cpp
187 lines (161 loc) · 4.72 KB
/
208. Implement Trie (Prefix Tree).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
//Runtime: 548 ms, faster than 5.04% of C++ online submissions for Implement Trie (Prefix Tree).
//Memory Usage: 23.9 MB, less than 100.00% of C++ online submissions for Implement Trie (Prefix Tree).
class Trie {
public:
set<string> words;
/** Initialize your data structure here. */
Trie() {
}
/** Inserts a word into the trie. */
void insert(string word) {
words.insert(word);
}
/** Returns if the word is in the trie. */
bool search(string word) {
return words.find(word)!=words.end();
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
for(set<string>::iterator it = words.begin(); it!=words.end(); it++){
if((*it).rfind(prefix, 0) == 0) return true;
}
return false;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/
//Runtime: 92 ms, faster than 59.89% of C++ online submissions for Implement Trie (Prefix Tree).
//Memory Usage: 48 MB, less than 30.86% of C++ online submissions for Implement Trie (Prefix Tree).
class TrieNode{
private:
vector<TrieNode*> links;
int R = 26;
bool isEnd = false;
public:
TrieNode() {
links = vector<TrieNode*>(R);
}
bool containsKey(char c){
return links[c-'a'] != NULL;
}
TrieNode* get(char c){
return links[c-'a'];
}
void put(char c, TrieNode* node){
links[c-'a'] = node;
}
void setEnd(){
isEnd = true;
}
bool getEnd(){
return isEnd;
}
};
class Trie {
private:
TrieNode *root;
// search a prefix or whole key in trie and
// returns the node where search ends
TrieNode* searchPrefix(string word){
TrieNode *node = root;
for(char c : word){
if(node->containsKey(c)){
node = node->get(c);
}else{
return NULL;
}
}
return node;
}
public:
/** Initialize your data structure here. */
Trie() {
root = new TrieNode();
}
/** Inserts a word into the trie. */
//time: O(m), space: O(m), m is the key length.
void insert(string word) {
TrieNode *node = root;
for(char c : word){
if(!node->containsKey(c)){
node->put(c, new TrieNode());
}
node = node->get(c);
}
node->setEnd();
}
/** Returns if the word is in the trie. */
//time: O(m), space: O(1)
bool search(string word) {
TrieNode *node = searchPrefix(word);
return node != NULL && node->getEnd();
}
/** Returns if there is any word in the trie that starts with the given prefix. */
//time: O(m), space: O(1)
bool startsWith(string prefix) {
TrieNode *node = searchPrefix(prefix);
return node != NULL;
}
};
//Runtime: 124 ms, faster than 45.63% of C++ online submissions for Implement Trie (Prefix Tree).
//Memory Usage: 48.1 MB, less than 16.67% of C++ online submissions for Implement Trie (Prefix Tree).
class TrieNode{
public:
bool isEnd;
vector<TrieNode*> children;
char c;
TrieNode(char c = '\0'){
isEnd = false;
this->c = c;
children = vector<TrieNode*>(26, nullptr);
}
};
class Trie {
public:
TrieNode* root;
/** Initialize your data structure here. */
Trie() {
root = new TrieNode();
}
/** Inserts a word into the trie. */
void insert(string word) {
TrieNode* node = root;
for(char c : word){
if(node->children[c-'a'] == nullptr){
node->children[c-'a'] = new TrieNode(c);
}
node = node->children[c-'a'];
}
node->isEnd = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
TrieNode* node = root;
for(char c : word){
if(node->children[c-'a'] == nullptr) return false;
node = node->children[c-'a'];
}
return node->isEnd;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
TrieNode* node = root;
for(char c : prefix){
if(node->children[c-'a'] == nullptr) return false;
node = node->children[c-'a'];
}
return true;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/