We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1c3b75b commit c57625dCopy full SHA for c57625d
matrix/medium/valid_sudoku.py
@@ -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
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
29
+ boxs[box_id].add(val)
30
+ return True
31
0 commit comments