forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
212-Word-Search-II.kt
68 lines (59 loc) · 1.92 KB
/
212-Word-Search-II.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 }
}
}