-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.py
More file actions
29 lines (22 loc) · 1.13 KB
/
Copy path36.py
File metadata and controls
29 lines (22 loc) · 1.13 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
26
27
28
29
#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.
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
row = collections.defaultdict(set)
col = collections.defaultdict(set)
sqr = collections.defaultdict(set)
for r in range(9):
for c in range(9):
if board[r][c] == ".":
continue
if board[r][c] in row[r] or board[r][c] in col[c] or board[r][c] in sqr[(r//3, c//3)]:
return False
row[r].add(board[r][c])
col[c].add(board[r][c])
sqr[(r//3, c//3)].add(board[r][c])
return True