fun findWords(board: Array<CharArray>, words: Array<String>): List<String> { if (board.isEmpty() || words.isEmpty()) { return emptyList() } val res = mutableListOf<String>() val root = Trie() words.forEach { root.insert(it) } for (i in 0 until board.size) { for (j in 0 until board[0].size) { dfs(board, root, i, j, res) } } return res }
} classTrie{
var children = HashMap<Char, Trie>() var word: String? = null
/** Inserts a word into the trie. */ fun insert(word: String){ var cur = this word.forEach { if (!cur.children.containsKey(it)) { cur.children[it] = Trie() } cur = cur.children[it]!! } cur.word = word } }