Skip to content

Commit c57625d

Browse files
committedApr 29, 2022
Matrix - medium - valid sudoku implementation
1 parent 1c3b75b commit c57625d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
 

‎matrix/medium/valid_sudoku.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def isValidSudoku(self, board: List[List[str]]) -> bool:
6+
n = 9
7+
rows = [set() for _ in range(n)]
8+
cols = [set() for _ in range(n)]
9+
boxs = [set() for _ in range(n)]
10+
11+
for x in range(n):
12+
for y in range(n):
13+
val = board[x][y]
14+
if val == ".":
15+
continue
16+
# check if already in row values
17+
if val in rows[x]:
18+
return False
19+
rows[x].add(val)
20+
# check if already in cols values
21+
if val in cols[y]:
22+
return False
23+
cols[y].add(val)
24+
25+
# check its in the box
26+
box_id = (x // 3) * 3 + y // 3
27+
if val in boxs[box_id]:
28+
return False
29+
boxs[box_id].add(val)
30+
return True
31+

0 commit comments

Comments
 (0)
Please sign in to comment.