-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.go
81 lines (74 loc) · 1.38 KB
/
Solution.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
type Trie struct {
children [26]*Trie
indexes []int
}
func newTrie() *Trie {
return &Trie{indexes: []int{}}
}
func (this *Trie) insert(word string, i int) {
node := this
for _, c := range word {
idx := c - 'a'
if node.children[idx] == nil {
node.children[idx] = newTrie()
}
node = node.children[idx]
node.indexes = append(node.indexes, i)
}
}
func (this *Trie) search(pref string) []int {
node := this
for _, c := range pref {
idx := c - 'a'
if node.children[idx] == nil {
return []int{}
}
node = node.children[idx]
}
return node.indexes
}
type WordFilter struct {
p *Trie
s *Trie
}
func Constructor(words []string) WordFilter {
p := newTrie()
s := newTrie()
for i, w := range words {
p.insert(w, i)
s.insert(reverse(w), i)
}
return WordFilter{p, s}
}
func (this *WordFilter) F(pref string, suff string) int {
a := this.p.search(pref)
b := this.s.search(reverse(suff))
if len(a) == 0 || len(b) == 0 {
return -1
}
i, j := len(a)-1, len(b)-1
for i >= 0 && j >= 0 {
if a[i] == b[j] {
return a[i]
}
if a[i] > b[j] {
i--
} else {
j--
}
}
return -1
}
func reverse(w string) string {
ww := []byte(w)
for i, j := 0, len(w)-1; i < j; i++ {
ww[i], ww[j] = ww[j], ww[i]
j--
}
return string(ww)
}
/**
* Your WordFilter object will be instantiated and called as such:
* obj := Constructor(words);
* param_1 := obj.F(pref,suff);
*/