-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearchTrieImpl.cpp
More file actions
54 lines (47 loc) · 1.3 KB
/
WordSearchTrieImpl.cpp
File metadata and controls
54 lines (47 loc) · 1.3 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
class Trie {
struct TrieNode{
struct TrieNode *children[26];
bool isEndOfWord;
};
TrieNode* node;
public:
Trie() {
node=new TrieNode();
}
void insert(string word) {
TrieNode* p_current = node;
for (auto& current_char: word)
{
int index = current_char - 'a';
if (!p_current->children[index]) {
p_current->children[index] = new TrieNode();
}
p_current = p_current->children[index];
}
p_current->isEndOfWord = true;
}
bool search(string word) {
TrieNode* p_current = node;
for (auto& current_char: word)
{
int index = current_char - 'a';
if (!p_current->children[index]) {
return false;
}
p_current = p_current->children[index];
}
return p_current->isEndOfWord;
}
bool startsWith(string prefix) {
TrieNode* p_current = node;
for (auto& current_char: prefix)
{
int index = current_char - 'a';
if (!p_current->children[index]) {
return false;
}
p_current = p_current->children[index];
}
return true;
}
};