package main
import"fmt"type Trie struct{
isWord bool
str map[string]*Trie
}/** Initialize your data structure here. */funcConstructor() Trie {var root =new(Trie)return*root
}/** Inserts a word into the trie. */func(this *Trie)Insert(word string){for_,v :=range word {if this.str[string(v)]==nil{
tmp :=new(Trie)if this.str ==nil{
this.str =make(map[string]*Trie)}
this.str[string(v)]= tmp
}
this = this.str[string(v)]}
this.isWord =true}/** Returns if the word is in the trie. */func(this *Trie)Search(word string)bool{for_,v :=range word {if this.str[string(v)]==nil{returnfalse}
this = this.str[string(v)]}return this.isWord
}/** Returns if there is any word in the trie that starts with the given prefix. */func(this *Trie)StartsWith(prefix string)bool{for_,v :=range prefix {if this.str[string(v)]==nil{returnfalse}
this = this.str[string(v)]}returntrue}/**
* Your Trie object will be instantiated and called as such:
* obj := Constructor();
* obj.Insert(word);
* param_2 := obj.Search(word);
* param_3 := obj.StartsWith(prefix);
*/funcmain(){var res bool
this :=Constructor()
this.Insert("app")
this.Insert("apple")
this.Insert("beer")
this.Insert("add")
this.Insert("jam")
this.Insert("rental")
res = this.Search("apps")
fmt.Println(res)
res = this.Search("app")
res = this.Search("apple")
fmt.Println(res)
res = this.Search("ad")
fmt.Println(res)
res = this.StartsWith("app")
fmt.Println(res)