Fetch the repository succeeded.
type WordDictionary struct {
root *trie
}
func Constructor() WordDictionary {
return WordDictionary{new(trie)}
}
func (this *WordDictionary) AddWord(word string) {
this.root.insert(word)
}
func (this *WordDictionary) Search(word string) bool {
n := len(word)
var dfs func(int, *trie) bool
dfs = func(i int, cur *trie) bool {
if i == n {
return cur.isEnd
}
c := word[i]
if c != '.' {
child := cur.children[c-'a']
if child != nil && dfs(i+1, child) {
return true
}
} else {
for _, child := range cur.children {
if child != nil && dfs(i+1, child) {
return true
}
}
}
return false
}
return dfs(0, this.root)
}
type trie struct {
children [26]*trie
isEnd bool
}
func (t *trie) insert(word string) {
cur := t
for _, c := range word {
c -= 'a'
if cur.children[c] == nil {
cur.children[c] = new(trie)
}
cur = cur.children[c]
}
cur.isEnd = true
}
/**
* Your WordDictionary object will be instantiated and called as such:
* obj := Constructor();
* obj.AddWord(word);
* param_2 := obj.Search(word);
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。