Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions kotlin/211-Design-Add-And-Search-Words-Data-Structure.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class WordDictionary() {
data class TrieNode(var isLeaf: Boolean = false, val children: MutableMap<Char, TrieNode> = 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 }
}
}
68 changes: 68 additions & 0 deletions kotlin/212-Word-Search-II.kt
Original file line number Diff line number Diff line change
@@ -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<CharArray>, words: Array<String>): List<String> {
val nRows = board.size
val nCols = board[0].size
val root = buildTrie(words)
val ans = mutableListOf<String>()
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<CharArray>, node: TrieNode, res: MutableList<String>){
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<String>): 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<TrieNode?> = Array(26){ null }
}
}