forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
53 lines (49 loc) · 912 Bytes
/
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
type Trie struct {
children [26]*Trie
idx int
}
func newTrie() *Trie {
return &Trie{idx: -1}
}
func (this *Trie) insert(word string, idx int) {
node := this
for _, c := range word {
idx := c - 'a'
if node.children[idx] == nil {
node.children[idx] = newTrie()
}
node = node.children[idx]
}
node.idx = idx
}
func (this *Trie) search(word string) []int {
node := this
var res []int
for _, c := range word {
idx := c - 'a'
if node.children[idx] == nil {
return res
}
node = node.children[idx]
if node.idx != -1 {
res = append(res, node.idx)
}
}
return res
}
func multiSearch(big string, smalls []string) [][]int {
tree := newTrie()
for i, s := range smalls {
tree.insert(s, i)
}
n := len(smalls)
ans := make([][]int, n)
for i := range big {
s := big[i:]
t := tree.search(s)
for _, idx := range t {
ans[idx] = append(ans[idx], i)
}
}
return ans
}