-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay14.cpp
More file actions
44 lines (35 loc) · 769 Bytes
/
Copy pathDay14.cpp
File metadata and controls
44 lines (35 loc) · 769 Bytes
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
class Trie {
vector < string > arr;
map< string, int > mp;
public:
/** Initialize your data structure here. */
Trie() {
arr.clear();
mp.clear();
}
/** Inserts a word into the trie. */
void insert(string word) {
arr.push_back(word);
mp[word]++;
}
/** Returns if the word is in the trie. */
bool search(string word) {
if (mp[word] > 0) {
return true;
}
else
return false;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
int len = prefix.length();
for (int i = 0; i < arr.size(); i++) {
string cur = arr[i].substr(0, len);
if (cur == prefix) {
return true;
}
cur.clear();
}
return false;
}
};