-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid_sudoku.py
32 lines (28 loc) · 1.01 KB
/
valid_sudoku.py
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
def isValidSudoku(board):
rows = [[] for _ in range(9)]
columns = [[] for _ in range(9)]
subbox = [[] for _ in range(9)]
for i in range(9):
for j in range(9):
if board[i][j] != ".":
num = int(board[i][j])
if num in rows[i]:
return False
if num in columns[j]:
return False
n = int(i/3)*3+int(j/3)
if num in subbox[n]:
return False
rows[i].append(num)
columns[j].append(num)
subbox[n].append(num)
return True
print(isValidSudoku([["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"]]))