-
Notifications
You must be signed in to change notification settings - Fork 0
/
Valid_Sudoku.py
30 lines (30 loc) · 1.07 KB
/
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
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
'''
1. No repetition within any row
2. No repetition within any column
3. No repetition within 9 3x3 subgrids
HashMap x -> Set of values in column x
HashMap y -> Set of values in row y
HashMap (x // 3, y // 3) -> Set of values in subgrid
Loop grid
Get value
Check if current value is in any hashmap
return false
Add to sets
return true
'''
cols = defaultdict(set)
rows = defaultdict(set)
subgrids = defaultdict(set)
lenX, lenY = len(board[0]), len(board)
for y in range(lenY):
for x in range(lenX):
val = board[y][x]
if val == ".": continue
subgrid = (x // 3, y // 3)
if val in cols[x] or val in rows[y] or val in subgrids[subgrid]: return False
cols[x].add(val)
rows[y].add(val)
subgrids[subgrid].add(val)
return True