-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidSudoku.py
More file actions
29 lines (24 loc) · 842 Bytes
/
Copy pathvalidSudoku.py
File metadata and controls
29 lines (24 loc) · 842 Bytes
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
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
SIZE = 9
BOX_SIZE = 3
rows = defaultdict(set)
cols = defaultdict(set)
boxes = defaultdict(set)
for i in range(SIZE):
for j in range(SIZE):
if board[i][j] == ".": continue
# check row
if board[i][j] in rows[i]:
return False
rows[i].add(board[i][j])
# check column
if board[i][j] in cols[j]:
return False
cols[j].add(board[i][j])
# check box
key = (i//BOX_SIZE, j//BOX_SIZE)
if board[i][j] in boxes[key]:
return False
boxes[key].add(board[i][j])
return True