Skip to main content

36. Valid Sudoku

https://leetcode.com/problems/valid-sudoku

Python

from collections import defaultdict


class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
col_table = defaultdict(set)
row_table = defaultdict(set)
cell_table = defaultdict(set)

for row, row_values in enumerate(board):
for col, value in enumerate(row_values):
if value == '.':
continue

cell_key = "{}{}".format(
row//3,
col//3
)

if value in col_table[col] or \
value in row_table[row] or \
value in cell_table[cell_key]:
return False

col_table[col].add(value)
row_table[row].add(value)
cell_table[cell_key].add(value)

return True