-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36_medium_[sense]_valid_sudoku.py
More file actions
71 lines (58 loc) · 2.26 KB
/
36_medium_[sense]_valid_sudoku.py
File metadata and controls
71 lines (58 loc) · 2.26 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from typing import List
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
def check_is_valid_val(board: List[List[str]], row: int, col: int) -> bool:
val = board[row][col]
# check row
for i in range(9):
if col == i:
continue
another_val = board[row][i]
if val == another_val:
return False
# check col
for i in range(9):
if row == i:
continue
another_val = board[i][col]
if val == another_val:
return False
# check 3x3 box
box_row, box_col = (row // 3, col // 3)
for r in range(3*box_row, 3*(box_row+1)):
for c in range(3*box_col, 3*(box_col+1)):
if r == row and c == col:
continue
another_val = board[r][c]
if val == another_val:
return False
return True
for row in range(9):
for col in range(9):
if board[row][col] != '.' and check_is_valid_val(board, row, col) is False:
return False
return True
if __name__ == '__main__':
s = Solution()
assert s.isValidSudoku([
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]) is True
assert s.isValidSudoku([
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]) is False