privatevar children = HashMap<Char, Trie>() privatevar isFinished = false
/** Inserts a word into the trie. */ funinsert(word: String) { var cur = this word.forEach { if (!cur.children.containsKey(it)) { cur.children[it] = Trie() } cur = cur.children[it]!! } cur.isFinished = true }
/** Returns if the word is in the trie. */ funsearch(word: String): Boolean { var cur = this word.forEach { if (!cur.children.containsKey(it)) { returnfalse } cur = cur.children[it]!! } return cur.isFinished }
/** Returns if there is any word in the trie that starts with the given prefix. */ funstartsWith(prefix: String): Boolean { var cur = this prefix.forEach { if (!cur.children.containsKey(it)) { returnfalse } cur = cur.children[it]!! } returntrue } }