forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
36-Valid-Sudoku.kt
31 lines (30 loc) · 1.24 KB
/
36-Valid-Sudoku.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
package kotlin
class Solution {
fun isValidSudoku(board: Array<CharArray>): Boolean {
val columnHashSets = Array(9) { HashSet<Int>() }
val rowHashSets = Array(9) { HashSet<Int>() }
val subMatrixHashSets = arrayOf(
arrayOf(HashSet<Int>(), HashSet<Int>(), HashSet<Int>()),
arrayOf(HashSet<Int>(), HashSet<Int>(), HashSet<Int>()),
arrayOf(HashSet<Int>(), HashSet<Int>(), HashSet<Int>())
)
for (i in 0 until 9) {
for (j in 0 until 9) {
// continue if is not a digit
if (!board[i][j].isDigit()) continue
val value = Character.getNumericValue(board[i][j])
// check column
if (value in columnHashSets[i]) return false
columnHashSets[i].add(value)
// check row
if (value in rowHashSets[j]) return false
rowHashSets[j].add(value)
// check sub matrix
if (value in subMatrixHashSets[j / 3][i / 3]) return false
// add the element to the hashset of the corresponding sub 3x3 matrix
subMatrixHashSets[j / 3][i / 3].add(value)
}
}
return true
}
}