From 9a441b13420cb7b25c399e9347ca4e70950d85d3 Mon Sep 17 00:00:00 2001 From: SubhadeepDas Date: Tue, 9 Aug 2022 01:11:56 +0530 Subject: [PATCH] Added 211-Design-Add-And-Search-Words-Data-Strcture.kt and 212-Word-Search-II.kt --- ...ign-Add-And-Search-Words-Data-Structure.kt | 25 +++++++ kotlin/212-Word-Search-II.kt | 68 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 kotlin/211-Design-Add-And-Search-Words-Data-Structure.kt create mode 100644 kotlin/212-Word-Search-II.kt diff --git a/kotlin/211-Design-Add-And-Search-Words-Data-Structure.kt b/kotlin/211-Design-Add-And-Search-Words-Data-Structure.kt new file mode 100644 index 000000000..ef8da4f27 --- /dev/null +++ b/kotlin/211-Design-Add-And-Search-Words-Data-Structure.kt @@ -0,0 +1,25 @@ +class WordDictionary() { + data class TrieNode(var isLeaf: Boolean = false, val children: MutableMap = mutableMapOf()) + + val root = TrieNode() + + fun addWord(word: String) { + var current = root + for (c in word) { + current = current.children.getOrPut(c) { TrieNode() } + } + current.isLeaf = true + } + + fun search(word: String): Boolean { + var candidates = listOf(root) + for (c in word) { + candidates = when (c) { + '.' -> candidates.flatMap { it.children.values } + else -> candidates.mapNotNull { it.children[c] } + } + if (candidates.isEmpty()) return false + } + return candidates.any { it.isLeaf } + } +} \ No newline at end of file diff --git a/kotlin/212-Word-Search-II.kt b/kotlin/212-Word-Search-II.kt new file mode 100644 index 000000000..238520d35 --- /dev/null +++ b/kotlin/212-Word-Search-II.kt @@ -0,0 +1,68 @@ +class Solution { + private companion object{ + const val IMPOSSIBLE: Char = '#' + val DIRS = intArrayOf(0, -1, 0, 1, 0) + } + + fun findWords(board: Array, words: Array): List { + val nRows = board.size + val nCols = board[0].size + val root = buildTrie(words) + val ans = mutableListOf() + for(row in 0 until nRows){ + for(col in 0 until nCols){ + backtrack(row, col, board, root, ans) + } + } + return ans + } + + private fun backtrack(row: Int, col: Int, board: Array, node: TrieNode, res: MutableList){ + val nRows = board.size + val nCols = board[0].size + + if(row < 0 || row >= nRows || col < 0 || col >= nCols) + return + + val hold = board[row][col] + val idx = hold.toInt() - 'a'.toInt(); + if(hold == IMPOSSIBLE || node.children[idx] == null){ + return + } + + var cur: TrieNode? = node + cur = cur!!.children[idx] + if(cur!!.word != null){ + res.add(cur.word!!) + cur.word = null + } + + board[row][col] = IMPOSSIBLE + for(d in 0 until 4){ + val r = row + DIRS[d] + val c = col + DIRS[d + 1] + backtrack(r, c, board, cur, res) + } + board[row][col] = hold + } + + private fun buildTrie(words: Array): TrieNode{ + val root = TrieNode() + for(word in words){ + var cur: TrieNode? = root + for(ch in word){ + val idx = ch.toInt() - 'a'.toInt() + if(cur!!.children[idx] == null) + cur.children[idx] = TrieNode() + + cur = cur.children[idx] + } + cur!!.word = word + } + return root + } + + private data class TrieNode(var word: String? = null){ + val children: Array = Array(26){ null } + } +} \ No newline at end of file