-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.go
46 lines (41 loc) · 875 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
package Solution
type WordDictionary struct {
trie map[int][]string
}
/** Initialize your data structure here. */
func Constructor() WordDictionary {
return WordDictionary{make(map[int][]string)}
}
func (this *WordDictionary) AddWord(word string) {
k := len(word)
if v, ok := this.trie[k]; ok {
this.trie[k] = append(v, word)
} else {
this.trie[k] = []string{word}
}
}
func (this *WordDictionary) Search(word string) bool {
k := len(word)
words, ok := this.trie[k]
if !ok {
return false
}
for _, cword := range words {
found := true
for i := 0; i < len(cword); i++ {
if word[i] != '.' && word[i] != cword[i] {
found = false
}
}
if found {
return true
}
}
return false
}
/**
* Your WordDictionary object will be instantiated and called as such:
* obj := Constructor();
* obj.AddWord(word);
* param_2 := obj.Search(word);
*/