You are viewing a single comment's thread. Return to all comments →
Kotlin Building Trie and checking on a fly
fun noPrefix(words: Array<String>): Unit { // Write your code here val tree = Node("") for (word in words) { var key = tree.text var parentNode = tree for (char in word) { key += char val node = parentNode.children.find { it.text == key } ?: Node(key) .also(parentNode.children::add) if (node.isWord) { println("BAD SET") println(word) return } parentNode = node } if (parentNode.children.isNotEmpty()) { println("BAD SET") println(word) return } parentNode.isWord = true } println("GOOD SET") } data class Node( var text: String, var isWord: Boolean = false, var children: MutableCollection<Node> = mutableListOf(), ) { }
Seems like cookies are disabled on this browser, please enable them to open this website
No Prefix Set
You are viewing a single comment's thread. Return to all comments →
Kotlin Building Trie and checking on a fly