-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path0036-valid-sudoku.py
44 lines (38 loc) · 1.45 KB
/
0036-valid-sudoku.py
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
33
34
35
36
37
38
39
40
41
42
43
44
from typing import List
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
def checkCols(board: List[List[str]], row: int) -> bool:
boardSet = set()
for i in range(9):
if board[row][i] != '.':
if board[row][i] in boardSet:
return False
boardSet.add(board[row][i])
return True
def checkRows(board: List[List[str]], col: int) -> bool:
boardSet = set()
for i in range(9):
if board[i][col] != '.':
if board[i][col] in boardSet:
return False
boardSet.add(board[i][col])
return True
def checkBox(board: List[List[str]], row: int, col: int) -> bool:
boardSet = set()
for i in range(row, row + 3):
for j in range(col, col + 3):
if board[i][j] != '.':
if board[i][j] in boardSet:
return False
boardSet.add(board[i][j])
return True
for i in range(9):
if not checkCols(board, i):
return False
if not checkRows(board, i):
return False
for i in range(0, 9, 3):
for j in range(0, 9, 3):
if not checkBox(board, i, j):
return False
return True