Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def isValidSudoku(self, board: List[List[str]]) -> bool:
- """ Solution: Use three dicts-to-sets to track the three ways that repetition
- is not allowed.
- """
- rows_to_seen = dict()
- cols_to_seen = dict()
- squares_to_seen = dict()
- for c in range(9):
- if c not in cols_to_seen:
- cols_to_seen[c] = set()
- for r in range(9):
- if r not in rows_to_seen:
- rows_to_seen[r] = set()
- if (c//3, r//3) not in squares_to_seen:
- squares_to_seen[(c//3, r//3)] = set()
- current_value = board[c][r]
- if current_value == '.':
- continue
- else:
- current_value = int(current_value)
- if current_value == 5:
- print(cols_to_seen[c])
- if current_value in cols_to_seen[c]:
- return False
- if current_value in rows_to_seen[r]:
- return False
- if current_value in squares_to_seen[(c//3, r//3)]:
- return False
- cols_to_seen[c].add(current_value)
- rows_to_seen[r].add(current_value)
- squares_to_seen[(c//3, r//3)].add(current_value)
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement