-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.valid-sudoku.py
More file actions
32 lines (27 loc) · 887 Bytes
/
36.valid-sudoku.py
File metadata and controls
32 lines (27 loc) · 887 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
30
31
32
#
# @lc app=leetcode id=36 lang=python3
#
# [36] Valid Sudoku
#
# @lc code=start
import collections
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
rows=collections.defaultdict(set)
cols=collections.defaultdict(set)
squares=collections.defaultdict(set)
for i in range(9):
for j in range(9):
if board[i][j]==".":
continue
if board[i][j] in rows[i]:
return False
if board[i][j] in cols[j]:
return False
if board[i][j] in squares[(i//3,j//3)]:
return False
rows[i].add(board[i][j])
cols[j].add(board[i][j])
squares[(i//3,j//3)].add(board[i][j])
return True
# @lc code=end