forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
103 lines (75 loc) · 2.27 KB
/
main.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
/// Source : https://leetcode.com/problems/implement-trie-prefix-tree/description/
/// Author : liuyubobobo
/// Time : 2017-11-05
#include <iostream>
#include <vector>
#include <map>
using namespace std;
/// Trie Recursive version
class Trie{
private:
struct Node{
map<char, int> next;
bool end = false;
};
vector<Node> trie;
public:
Trie(){
trie.clear();
trie.push_back(Node());
}
/** Inserts a word into the trie. */
void insert(const string& word){
insert(0, word, 0);
}
/** Returns if the word is in the trie. */
bool search(const string& word){
return search(0, word, 0);
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(const string& prefix) {
return startsWith(0, prefix, 0);
}
private:
void insert(int treeID, const string& word, int index){
if(index == word.size()) {
trie[treeID].end = true;
return;
}
if(trie[treeID].next.find(word[index]) == trie[treeID].next.end()){
trie[treeID].next[word[index]] = trie.size();
trie.push_back(Node());
}
insert(trie[treeID].next[word[index]], word, index + 1);
}
bool search(int treeID, const string& word, int index){
if(index == word.size())
return trie[treeID].end;
if(trie[treeID].next.find(word[index]) == trie[treeID].next.end())
return false;
return search(trie[treeID].next[word[index]], word, index + 1);
}
bool startsWith(int treeID, const string& prefix, int index){
if(index == prefix.size())
return true;
if(trie[treeID].next.find(prefix[index]) == trie[treeID].next.end())
return false;
return startsWith(trie[treeID].next[prefix[index]], prefix, index + 1);
}
};
void printBool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
Trie trie1;
trie1.insert("ab");
printBool(trie1.search("a")); // false
printBool(trie1.startsWith("a")); // true;
cout << endl;
// ---
Trie trie2;
trie2.insert("a");
printBool(trie2.search("a")); // true
printBool(trie2.startsWith("a")); // true;
return 0;
}