Storing a word is only half the contract - you also need to ask whether a word is present. Today you add Contains, which must tell a stored word apart from a path that merely exists as a prefix of another word.
Report whether an exact word was stored, distinguishing it from a mere prefix.
Contains walks the same edges Insert did. If at any character there is no
child, the word was never stored and you return false immediately. If the walk
finishes, you have found the path - but a path is not a word. ca is a path
in a trie holding cat and car, yet nobody stored ca, so its node’s end
flag is false and Contains("ca") must be false.
This is the whole point of the end marker: without it a trie could only answer
“is this a prefix of something?”, never “is this an actual word?”. The next
lesson looks at what happens where words branch, and later chapters turn “is this
a prefix?” into the completions that power autocomplete.
func (t *Trie) Contains(word string) bool {cur := t.rootfor _, r := range word {next, ok := cur.children[r]if !ok {return false // ran off the tree - no such path}cur = next}// the path exists; it is a WORD only if this node ends onereturn cur.end}