Skip to content

Instantly share code, notes, and snippets.

@jlgm
Created June 30, 2021 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlgm/9a48f58408a14b188d8962364f5cb79c to your computer and use it in GitHub Desktop.
Save jlgm/9a48f58408a14b188d8962364f5cb79c to your computer and use it in GitHub Desktop.
type trieNode struct {
childrens [26]*trieNode
isWordEnd bool
}
type trie struct {
root *trieNode
}
func initTrie() *trie {
return &trie{
root: &trieNode{},
}
}
func (t *trie) insert(word *string) {
wordLen := len(*word)
current := t.root
for i := wordLen-1; i >= 0; i-- {
index := (*word)[i] - 'a'
if current.childrens[index] == nil {
current.childrens[index] = &trieNode{}
}
current = current.childrens[index]
}
current.isWordEnd = true
}
func (t *trie) find(word *[]byte) bool {
wordLen := len(*word)
cur := t.root
for i := wordLen-1; i >= 0; i-- {
idx := (*word)[i]-'a'
if cur.childrens[idx] == nil {
return false
} else if cur.childrens[idx].isWordEnd {
return true
}
cur = cur.childrens[idx]
}
return cur.isWordEnd
}
type StreamChecker struct {
stream []byte
trie *trie
}
func Constructor(words []string) StreamChecker {
stream := []byte("")
trie := initTrie()
for _, w := range words {
trie.insert(&w)
}
return StreamChecker{trie: trie, stream: stream}
}
func (this *StreamChecker) Query(letter byte) bool {
this.stream = append(this.stream, letter)
return this.trie.find(&this.stream)
}
/**
* Your StreamChecker object will be instantiated and called as such:
* obj := Constructor(words);
* param_1 := obj.Query(letter);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment