Advertisement
nathanwailes

LeetCode 36 - Valid Sudoku - 2023.10.03 solution

Oct 2nd, 2023
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. class Solution:
  2.     def isValidSudoku(self, board: List[List[str]]) -> bool:
  3.         """ Solution: Use three dicts-to-sets to track the three ways that repetition
  4.        is not allowed.
  5.        """
  6.         rows_to_seen = dict()
  7.         cols_to_seen = dict()
  8.         squares_to_seen = dict()
  9.  
  10.         for c in range(9):
  11.             if c not in cols_to_seen:
  12.                 cols_to_seen[c] = set()
  13.             for r in range(9):
  14.                 if r not in rows_to_seen:
  15.                     rows_to_seen[r] = set()
  16.                 if (c//3, r//3) not in squares_to_seen:
  17.                     squares_to_seen[(c//3, r//3)] = set()
  18.                 current_value = board[c][r]
  19.                 if current_value == '.':
  20.                     continue
  21.                 else:
  22.                     current_value = int(current_value)
  23.                 if current_value == 5:
  24.                     print(cols_to_seen[c])
  25.                 if current_value in cols_to_seen[c]:
  26.                     return False
  27.                 if current_value in rows_to_seen[r]:
  28.                     return False
  29.                 if current_value in squares_to_seen[(c//3, r//3)]:
  30.                     return False
  31.                 cols_to_seen[c].add(current_value)
  32.                 rows_to_seen[r].add(current_value)
  33.                 squares_to_seen[(c//3, r//3)].add(current_value)
  34.         return True
  35.  
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement