We can use a single set to store "seen" strings for each row, column, and box. For example, "5 in row 0", "5 in col 1", and "5 in box 0-0".
- Initialize
seen = set(). - Iterate through
randcfrom 0 to 8:- If
board[r][c] != '.':val = board[r][c].- Create tokens:
f"{val} in row {r}",f"{val} in col {c}",f"{val} in box {r//3}-{c//3}". - If any token is already in
seen, return False. - Add all tokens to
seen.
- If
- Return True.
- Time Complexity: O(1) (fixed 9x9 board).
- Space Complexity: O(1).
def is_valid_sudoku(board):
seen = set()
for r in range(9):
for c in range(9):
val = board[r][c]
if val != '.':
if (val, 'r', r) in seen or \
(val, 'c', c) in seen or \
(val, 'b', r//3, c//3) in seen:
return False
seen.add((val, 'r', r))
seen.add((val, 'c', c))
seen.add((val, 'b', r//3, c//3))
return True