-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path208.实现-trie-前缀树.go
105 lines (78 loc) · 1.85 KB
/
208.实现-trie-前缀树.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
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
package main
import "fmt"
// @lc code=start
type Trie struct {
isWord bool
children [26]*Trie
}
/** Initialize your data structure here. */
func Constructor() Trie {
return Trie{}
}
/** Inserts a word into the trie. */
func (this *Trie) Insert(word string) {
cur := this
for index, c := range word {
n := c - 'a'
if cur.children[n] == nil {
cur.children[n] = new(Trie)
}
cur = cur.children[n]
if index == len(word) - 1 {
cur.isWord = true
}
}
}
/** Returns if the word is in the trie. */
func (this *Trie) Search(word string) bool {
cur := this
for index, c := range word {
n := c - 'a'
if cur.children[n] == nil {
return false
}
if index == len(word) -1 {
return true
}
cur = cur.children[n]
}
return true
}
/** Returns if there is any word in the trie that starts with the given prefix. */
func (this *Trie) StartsWith(prefix string) bool {
cur := this
position := 0
for index, c := range prefix {
n := c - 'a'
if cur.children[n] == nil {
break
}
position++
if index == len(prefix) -1 {
break
}
cur = cur.children[n]
}
return position == len(prefix) && cur.isWord == true
}
func main(){
obj := Constructor();
word1 := "hello"
obj.Insert(word1);
param_11 := obj.Search(word1);
Parma_12 := obj.StartsWith(word1)
fmt.Printf("search is %t, startsWith is %t\n", param_11, Parma_12)
word2 := "hex"
param_21 := obj.Search(word2)
param_22 := obj.StartsWith(word2)
fmt.Printf("search is %t, startsWith is %t\n", param_21, param_22)
// param_3 := obj.StartsWith(prefix);
word3 := "he"
param_31 := obj.Search(word3)
param_32 := obj.StartsWith(word3)
fmt.Printf("search is %t, startsWith is %t\n", param_31, param_32)
obj.Insert(word2)
param_31 = obj.Search(word2)
param_32 = obj.StartsWith(word2)
fmt.Printf("search is %t, startsWith is %t\n", param_31, param_32)
}