MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / Insert

Method Insert

implement_trie_208/solution.go:21–39  ·  view source on GitHub ↗

Inserts a word into the trie

(word string)

Source from the content-addressed store, hash-verified

19
20// Inserts a word into the trie
21func (this *Trie) Insert(word string) {
22 curr := this.root
23 for i := 0; i < len(word); i++ {
24 slot := word[i] - 'a'
25
26 // If the child doesn't exist, create it
27 if curr.children[slot] == nil {
28 curr.children[slot] = &trieNode{
29 char: word[i],
30 }
31 }
32
33 // Advance curr to the slot
34 curr = curr.children[slot]
35 }
36
37 // Mark the last node as the end of an inserted word
38 curr.end = true
39}
40
41// Returns true if the word is in the trie
42func (this *Trie) Search(word string) bool {

Callers 1

TestTrieFunction · 0.80

Calls

no outgoing calls

Tested by 1

TestTrieFunction · 0.64