-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidSoduko.py
More file actions
35 lines (30 loc) · 1.26 KB
/
validSoduko.py
File metadata and controls
35 lines (30 loc) · 1.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
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
#use a hashmap col -> set for every row, every column
#use a hashmap for each block
#index rows and columns as blocks
#Hashmap (bi, bj) -> HashSet
#0 1 2
#1
#2
#take actual index and divide it by 3 (round down) to get block index
cols = collections.defaultdict(set)
rows = collections.defaultdict(set)
blocks = collections.defaultdict(set) #key = (r/3, c/3)
for r in range(9):
for c in range(9):
if board[r][c] == ".":
continue
if (board[r][c] in rows[r] or
board[r][c] in cols[c] or
board[r][c] in blocks[(r//3,c//3)]):
return False
cols[c].add(board[r][c])
rows[r].add(board[r][c])
blocks[(r//3,c//3)].add(board[r][c])
return True
#can i optimize further by using a hasDuplicates or isUnique
#or some other way of finding out if there are dupes? to avoid
#the O(n^2)
#Time complexity: O(n^2) where n is size of array
#Space complexity: O(n^2) worst case if we store every square