-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidSudoku.py
More file actions
72 lines (61 loc) · 2.26 KB
/
validSudoku.py
File metadata and controls
72 lines (61 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
72
# https://leetcode.com/problems/valid-sudoku/?envType=study-plan&id=data-structure-i
class Solution:
def isSafe(self,l,board):
for sub in l:
d = {}
row,col = sub[0],sub[1]
for i in range(row,row+3):
for j in range(col,col+3):
if board[i][j] != '.' and board[i][j] in d:
d[board[i][j]]+=1
else:
d[board[i][j]] = 1
for ct in d:
if d[ct]>1:
return False
return True
def isValidSudoku(self, board):
l = [[0,0],[3,0],[6,0],[0,3],[3,3],[3,6],[0,6],[6,6],[6,3]]
if not self.isSafe(l, board):
return False
for i in range(9):
for j in range(9):
digit = board[i][j]
ctDig = 0
for row in range(9):
if digit!='.' and digit == board[i][row]:
ctDig+=1
if ctDig>=2:
return False
ctDig = 0
for col in range(9):
if digit!='.' and digit == board[col][j]:
ctDig+=1
# print(digit,board[col][j])
if ctDig>=2:
return False
return True
board = [[".",".",".",".",".",".","5",".","."]
,[".",".",".",".",".",".",".",".","."]
,[".",".",".",".",".",".",".",".","."]
,["9","3",".",".","2",".","4",".","."]
,[".",".","7",".",".",".","3",".","."]
,[".",".",".",".",".",".",".",".","."]
,[".",".",".","3","4",".",".",".","."]
,[".",".",".",".",".","3",".",".","."]
,[".",".",".",".",".","5","2",".","."]]
# board = [[".",".",".",".","5",".",".","1","."]
# ,[".","4",".","3",".",".",".",".","."]
# ,[".",".",".",".",".","3",".",".","1"]
# ,["8",".",".",".",".",".",".","2","."]
# ,[".",".","2",".","7",".",".",".","."]
# ,[".","1","5",".",".",".",".",".","."]
# ,[".",".",".",".",".","2",".",".","."]
# ,[".","2",".","9",".",".",".",".","."]
# ,[".",".","4",".",".",".",".",".","."]]
# for i in range(9):
# for j in range(9):
# print(board[i][j],end=' ')
# print()
s = Solution()
print(s.isValidSudoku(board))