MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / insert

Method insert

kotlin/Tries/DesignATrie.kt:9–22  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7 val root = TrieNode()
8
9 fun insert(word: String) {
10 var node = root
11 for (c in word.toCharArray()) {
12 // For each character in the word, if it's not a child of
13 // the current node, create a new TrieNode for that
14 // character.
15 if (c !in node.children) {
16 node.children[c] = TrieNode()
17 }
18 node = node.children[c]!!
19 }
20 // Mark the last node as the end of a word.
21 node.isWord = true
22 }
23
24 fun search(word: String): Boolean {
25 var node = root

Calls 1

TrieNodeClass · 0.70

Tested by

no test coverage detected