Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def isValidSudoku(self, board: List[List[str]]) -> bool:
- """ How to solve it: Iterate through the cells once, maintaining three different dicts of sets,
- one dict for the rows, one dict for the columns, and one for the squares. If you hit a duplicate,
- return False, otherwise if you get to the end return True.
- """
- row_sets = defaultdict(set)
- col_sets = defaultdict(set)
- square_sets = defaultdict(set)
- for r in range(9):
- for c in range(9):
- if board[r][c] == '.':
- continue
- if board[r][c] in col_sets[c] or board[r][c] in row_sets[r] or \
- board[r][c] in square_sets[(r//3, c//3)]:
- return False
- else:
- row_sets[r].add(board[r][c])
- col_sets[c].add(board[r][c])
- square_sets[(r//3, c//3)].add(board[r][c])
- return True
Advertisement
Add Comment
Please, Sign In to add comment