Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits 1-9 without repetition.
- Each column must contain the digits 1-9 without repetition.
- Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
Note:
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
9 x 9 스도쿠 보드가 유효한지 확인합니다. 다음 규칙에 따라 채워진 셀만 검증하면 됩니다.
1. 각 행에는 반복 없이 숫자 1~9가 포함되어야 합니다.
2. 각 열에는 반복 없이 숫자 1~9가 포함되어야 합니다.
3. 그리드의 9개의 3 x 3 하위 상자 각각에는 반복 없이 숫자 1~9가 포함되어야 합니다.
메모:
- 스도쿠 보드(부분적으로 채워짐)는 유효할 수 있지만 반드시 풀 수 있는 것은 아닙니다.
- 언급된 규칙에 따라 채워진 셀만 검증하면 됩니다.
Example 1:
Input: board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true
Example 2:
Input: board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Constraints:
- board.length == 9
- board[i].length == 9
- board[i][j] is a digit 1-9 or '.'.
코드 1
class Solution {
fun isValidSudoku(board: Array<CharArray>): Boolean {
val rowMap = HashMap<Int, HashSet<Char>>()
val colMap = HashMap<Int, HashSet<Char>>()
val squareMap = HashMap<Pair<Int, Int>, HashSet<Char>>()
for (i in board.indices) {
for (j in board[i].indices) {
val now = board[i][j]
if (now != '.') {
// if (!rowMap.containsKey(i)) {
if (i !in rowMap) {
rowMap[i] = HashSet()
}
if (j !in colMap) {
colMap[j] = HashSet()
}
if (Pair(i / 3, j / 3) !in squareMap) {
squareMap[Pair(i / 3, j / 3)] = HashSet()
}
if (rowMap[i]!!.contains(now) ||
colMap[j]!!.contains(now) ||
squareMap[Pair(i / 3, j / 3)]!!.contains(now)
) {
return false
}
rowMap[i]!!.add(now)
colMap[j]!!.add(now)
squareMap[Pair(i / 3, j / 3)]!!.add(now)
}
}
}
return true
}
}
코드 2
class Solution {
fun isValidSudoku(board: Array<CharArray>): Boolean {
val rowSet = Array(9) { HashSet<Char>() }
val colSet = Array(9) { HashSet<Char>() }
val squareSet = Array(9) { HashSet<Char>() }
for (i in board.indices) {
for (j in board[i].indices) {
val now = board[i][j]
if (now != '.') {
val squareIndex = (i / 3) * 3 + (j / 3)
if (now in rowSet[i] ||
now in colSet[j] ||
now in squareSet[squareIndex]
) {
return false
}
rowSet[i].add(now)
colSet[j].add(now)
squareSet[squareIndex].add(now)
}
}
}
return true
}
}
'LeetCode > Array & Hashing' 카테고리의 다른 글
[LeetCode][Kotlin] 75. Sort Colors (0) | 2024.10.18 |
---|---|
[LeetCode][Kotlin] String Encode and Decode (0) | 2024.10.18 |
[LeetCode][Kotlin] 349. Intersection of Two Arrays (0) | 2024.10.18 |