File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ class Trie {
3
+ vector < string > arr;
4
+ map< string, int > mp;
5
+ public:
6
+ /* * Initialize your data structure here. */
7
+ Trie () {
8
+
9
+ arr.clear ();
10
+ mp.clear ();
11
+
12
+ }
13
+
14
+ /* * Inserts a word into the trie. */
15
+ void insert (string word) {
16
+ arr.push_back (word);
17
+ mp[word]++;
18
+
19
+ }
20
+
21
+ /* * Returns if the word is in the trie. */
22
+ bool search (string word) {
23
+ if (mp[word] > 0 ) {
24
+ return true ;
25
+ }
26
+ else
27
+ return false ;
28
+ }
29
+
30
+ /* * Returns if there is any word in the trie that starts with the given prefix. */
31
+ bool startsWith (string prefix) {
32
+
33
+ int len = prefix.length ();
34
+
35
+ for (int i = 0 ; i < arr.size (); i++) {
36
+ string cur = arr[i].substr (0 , len);
37
+ if (cur == prefix) {
38
+ return true ;
39
+ }
40
+ cur.clear ();
41
+ }
42
+ return false ;
43
+ }
44
+ };
You can’t perform that action at this time.
0 commit comments