deno.land / x / masx200_leetcode_test@10.6.5 / implement-trie-prefix-tree / index.go

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
package implement_trie_prefix_tree
type Trie struct { children map[rune]*Trie isEnd bool
words map[string]bool}
func Constructor() Trie { return Trie{children: make(map[rune]*Trie), words: make(map[string]bool)}}
func (t *Trie) Insert(word string) {
t.words[word] = true node := t for _, ch := range word {
if node.children[ch] == nil { var nt = Constructor() node.children[ch] = &nt } node = node.children[ch] } node.isEnd = true}
func (t *Trie) SearchPrefix(prefix string) *Trie { node := t for _, ch := range prefix {
if node.children[ch] == nil { return nil } node = node.children[ch] } return node}
func (t *Trie) Search(word string) bool { return t.words[word]}
func (t *Trie) StartsWith(prefix string) bool {
if t.words[prefix] {
return true } return t.SearchPrefix(prefix) != nil}
masx200_leetcode_test

Version Info

Tagged at
a year ago