Skip to content

Commit 336cae4

Browse files
authored
Added Python solution to 36. Valid Sudoku (#139)
1 parent bf1ed65 commit 336cae4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def isValidSudoku(self, board: List[List[str]]) -> bool:
3+
rows = collections.defaultdict(set)
4+
cols = collections.defaultdict(set)
5+
sqrs = collections.defaultdict(set)
6+
7+
for i in range(9):
8+
for j in range(9):
9+
if board[i][j] == ".":
10+
continue
11+
12+
if (board[i][j] in rows[i] or
13+
board[i][j] in cols[j] or
14+
board[i][j] in sqrs[(i // 3, j // 3)]):
15+
return False
16+
17+
rows[i].add(board[i][j])
18+
cols[j].add(board[i][j])
19+
sqrs[(i // 3, j // 3)].add(board[i][j])
20+
21+
return True

0 commit comments

Comments
 (0)