-
Notifications
You must be signed in to change notification settings - Fork 481
/
0208.cpp
61 lines (54 loc) · 1.35 KB
/
0208.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
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Node
{
public:
Node() : isWord(false){}
~Node()
{
for (const auto& item : next) delete item.second;
}
bool isWord;
unordered_map<char, Node*> next;
};
class Trie
{
public:
/** Initialize your data structure here. */
Trie() : _root(new Node()) {}
~Trie() { delete _root; }
/** Inserts a word into the trie. */
void insert(string word)
{
auto cur = _root;
for (auto c : word)
{
if (!(cur->next).count(c)) // or if (cur->next[c] == nullptr)
cur->next[c] = new Node();
cur = cur->next[c];
}
if (!cur->isWord) cur->isWord = true;
}
/** Returns if the word is in the trie. */
bool search(string word)
{
auto cur = _search(word);
return cur != nullptr and cur->isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix)
{
return _search(prefix) != nullptr;
}
private:
Node* _root;
Node* _search(string word)
{
auto cur = _root;
for (auto c : word)
{
if (!cur->next.count(c)) return nullptr;
cur = cur->next[c];
}
return cur;
}
};