(Medium #36) Valid Sudoku
Problem
Determine if a 9x9 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 9
3x3
sub-boxes of the grid must contain the digits 1-9
without repetition.

A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
Example 1:
Input:
[
["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:
[
["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.
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.
- The given board contain only digits
1-9
and the character '.'
.
- The given board size is always
9x9
.
Determine if a 9x9 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 9
3x3
sub-boxes of the grid must contain the digits1-9
without repetition.

A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character
'.'
.
Example 1:
Input: [ ["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: [ ["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.
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.
- The given board contain only digits
1-9
and the character'.'
. - The given board size is always
9x9
.
My solution
class Solution:
def isValidSudoku(self, board):
r, c = len(board), len(board[0])
for i in range(r):
for j in range(c):
if not self.isValid(i, j, board):
return False
return True
def isValid(self, x, y, board):
if board[x][y] == ".":
return True
tmp = board[x][y]#; board[x][y] = "#"
r, c = len(board), len(board[0])
count = 0
for i in range(r):
if board[i][y] == tmp:
count+=1
if count > 1:
return False
count = 0
for j in range(c):
if board[x][j] == tmp:
count+=1
if count > 1:
return False
count = 0
for i in range(int(x/3)*3, int(x/3)*3+3):
for j in range(int(y/3)*3, int(y/3)*3+3):
if board[i][j] == tmp:
count+=1
if count > 1:
return False
return True
Runtime: 112ms, Memory: 13.7MB
def isValidSudoku(self, board):
r, c = len(board), len(board[0])
for i in range(r):
for j in range(c):
if not self.isValid(i, j, board):
return False
return True
def isValid(self, x, y, board):
if board[x][y] == ".":
return True
tmp = board[x][y]#; board[x][y] = "#"
r, c = len(board), len(board[0])
count = 0
for i in range(r):
if board[i][y] == tmp:
count+=1
if count > 1:
return False
count = 0
for j in range(c):
if board[x][j] == tmp:
count+=1
if count > 1:
return False
count = 0
for i in range(int(x/3)*3, int(x/3)*3+3):
for j in range(int(y/3)*3, int(y/3)*3+3):
if board[i][j] == tmp:
count+=1
if count > 1:
return False
return True
문제의 불완전함
(예시) 입력:
[
["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"],
[".",".","1",".","8",".",".","7","9"]
]
해석:
1. 위의 그림과 같이 9번째 행에서 1이 두 개가 존재해야하므로 실제 스도쿠에서는 False이나 코드 상으로는 True가 나오는게 정답임.
2. 내가 아는 스도쿠를 푼다는 개념에서는 틀리지만 하지만 애초에 문제에서 위의 조건 3개만 검사하라 했으니 어떻게 보면 틀린 것도 아니긴 해보이긴 함.
3. 나와 비슷한 생각을 한 사람들이 real valid 라는 키워드로 검색해보면 다음과 같이 엄청 많다. (하나의 예)
4. Hard 문제에 스도쿠를 직접 풀어서 결과를 내야하는 문제가 있다. 여기서는 문제가 unique 솔루션이있다고 가정을 한다. 이 스도쿠 solving 알고리즘을 이용해서 정말로 스도쿠를 풀 수 있는지에 따른 것을 도출할 수 있다고 생각한다.
2. 내가 아는 스도쿠를 푼다는 개념에서는 틀리지만 하지만 애초에 문제에서 위의 조건 3개만 검사하라 했으니 어떻게 보면 틀린 것도 아니긴 해보이긴 함.
3. 나와 비슷한 생각을 한 사람들이 real valid 라는 키워드로 검색해보면 다음과 같이 엄청 많다. (하나의 예)
4. Hard 문제에 스도쿠를 직접 풀어서 결과를 내야하는 문제가 있다. 여기서는 문제가 unique 솔루션이있다고 가정을 한다. 이 스도쿠 solving 알고리즘을 이용해서 정말로 스도쿠를 풀 수 있는지에 따른 것을 도출할 수 있다고 생각한다.
댓글
댓글 쓰기