-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1804-ImplementTrieII.go
More file actions
136 lines (123 loc) · 4.52 KB
/
1804-ImplementTrieII.go
File metadata and controls
136 lines (123 loc) · 4.52 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
// 1804. Implement Trie II (Prefix Tree)
// A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings.
// There are various applications of this data structure, such as autocomplete and spellchecker.
// Implement the Trie class:
// Trie()
// Initializes the trie object.
// void insert(String word)
// Inserts the string word into the trie.
// int countWordsEqualTo(String word)
// Returns the number of instances of the string word in the trie.
// int countWordsStartingWith(String prefix)
// Returns the number of strings in the trie that have the string prefix as a prefix.
// void erase(String word)
// Erases the string word from the trie.
// Example 1:
// Input
// ["Trie", "insert", "insert", "countWordsEqualTo", "countWordsStartingWith", "erase", "countWordsEqualTo", "countWordsStartingWith", "erase", "countWordsStartingWith"]
// [[], ["apple"], ["apple"], ["apple"], ["app"], ["apple"], ["apple"], ["app"], ["apple"], ["app"]]
// Output
// [null, null, null, 2, 2, null, 1, 1, null, 0]
// Explanation
// Trie trie = new Trie();
// trie.insert("apple"); // Inserts "apple".
// trie.insert("apple"); // Inserts another "apple".
// trie.countWordsEqualTo("apple"); // There are two instances of "apple" so return 2.
// trie.countWordsStartingWith("app"); // "app" is a prefix of "apple" so return 2.
// trie.erase("apple"); // Erases one "apple".
// trie.countWordsEqualTo("apple"); // Now there is only one instance of "apple" so return 1.
// trie.countWordsStartingWith("app"); // return 1
// trie.erase("apple"); // Erases "apple". Now the trie is empty.
// trie.countWordsStartingWith("app"); // return 0
// Constraints:
// 1 <= word.length, prefix.length <= 2000
// word and prefix consist only of lowercase English letters.
// At most 3 * 10^4 calls in total will be made to insert, countWordsEqualTo, countWordsStartingWith, and erase.
// It is guaranteed that for any function call to erase, the string word will exist in the trie.
import "fmt"
type Trie struct {
children [26]*Trie
pre int
end int
}
func Constructor() Trie {
return Trie{}
}
func (t *Trie) Insert(s string) {
o := t
for _, b := range s {
b -= 'a'
if o.children[b] == nil {
o.children[b] = &Trie{}
}
o = o.children[b]
o.pre++
}
o.end++
}
func (t *Trie) CountWordsEqualTo(s string) int {
o := t
for _, b := range s {
o = o.children[b-'a']
if o == nil {
return 0
}
}
return o.end
}
func (t *Trie) CountWordsStartingWith(s string) int {
o := t
for _, b := range s {
o = o.children[b-'a']
if o == nil {
return 0
}
}
return o.pre
}
func (t *Trie) Erase(s string) {
o := t
for _, b := range s {
o = o.children[b-'a']
o.pre--
}
o.end--
}
/**
* Your Trie object will be instantiated and called as such:
* obj := Constructor();
* obj.Insert(word);
* param_2 := obj.CountWordsEqualTo(word);
* param_3 := obj.CountWordsStartingWith(prefix);
* obj.Erase(word);
*/
func main() {
// Trie trie = new Trie();
obj := Constructor()
fmt.Println(obj)
// trie.insert("apple"); // Inserts "apple".
obj.Insert("apple")
fmt.Println(obj)
// trie.insert("apple"); // Inserts another "apple".
obj.Insert("apple")
fmt.Println(obj)
// trie.countWordsEqualTo("apple"); // There are two instances of "apple" so return 2.
fmt.Println(obj.CountWordsEqualTo("apple")) // 2
// trie.countWordsStartingWith("app"); // "app" is a prefix of "apple" so return 2.
fmt.Println(obj.CountWordsEqualTo("app")) // 2
// trie.erase("apple"); // Erases one "apple".
obj.Erase("apple")
fmt.Println(obj)
// trie.countWordsEqualTo("apple"); // Now there is only one instance of "apple" so return 1.
fmt.Println(obj.CountWordsEqualTo("apple")) // 1
// trie.countWordsStartingWith("app"); // return 1
fmt.Println(obj.CountWordsEqualTo("app")) // 1
// trie.erase("apple"); // Erases "apple". Now the trie is empty.
obj.Erase("apple")
fmt.Println(obj)
// trie.countWordsStartingWith("app"); // return 0
fmt.Println(obj.CountWordsEqualTo("apple")) // 0
// trie.countWordsStartingWith("app"); // return 0
fmt.Println(obj.CountWordsEqualTo("app")) // 0
}