-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidSudoku.py
More file actions
25 lines (19 loc) · 1.3 KB
/
validSudoku.py
File metadata and controls
25 lines (19 loc) · 1.3 KB
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
import collections
def isValidSudoku(board: list[list[str]]) -> bool:
cols = collections.defaultdict(list)
rows = collections.defaultdict(list)
squares = collections.defaultdict(list)
for r in range(9):
for c in range(9):
if board[r][c] != ".":
if (board[r][c] in rows[r] or
board[r][c] in cols[c] or
board[r][c] in squares[(r // 3, c // 3)]):
return False
cols[c].append(board[r][c])
rows[r].append(board[r][c])
squares[(r // 3, c // 3)].append(board[r][c])
return True
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"]]
print(isValidSudoku(board))