-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0079-word-search.kt
50 lines (36 loc) · 1.23 KB
/
0079-word-search.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
class Solution {
var ROWS: Int = 0
var COLS: Int = 0
fun exist(board: Array<CharArray>, word: String): Boolean {
ROWS = board.count()
COLS = board[0].count()
for (row in 0 until ROWS) {
for (col in 0 until COLS) {
if (dfs(board, row, col, word, 0))
return true
}
}
return false
}
fun dfs(board: Array<CharArray>, row: Int, col: Int, word: String, indx: Int): Boolean {
/* Step 1). check the bottom case. */
if (indx == word.count())
return true
/* Step 2). Check the boundaries. */
if (row < 0 || row == ROWS || col < 0 || col == COLS || board[row][col] != word[indx])
return false
// mark the path before the next exploration
board[row][col] = '#'
var found = false
val rowOffsets = intArrayOf(0, 1, 0, -1)
val colOffsets = intArrayOf(1, 0, -1, 0)
for (i in 0..3) {
found = dfs(board, row + rowOffsets[i], col + colOffsets[i], word, indx + 1)
if (found)
break
}
/* Step 4). clean up and return the result. */
board[row][col] = word[indx]
return found
}
}