We use hash sets to track whether a digit has already appeared in a particular row, column, or 3x3 box.
- Initialize 9 sets for rows, 9 for columns, and 9 for boxes.
- Iterate through every cell
(r, c)in the 9x9 board. - If value is not
.:- Calculate box index:
(r // 3) * 3 + (c // 3). - Check if the value exists in
rows[r],cols[c], orboxes[idx]. - If yes, return False.
- Else, add it to all three sets.
- Calculate box index:
- If loop finishes, return True.
- Time Complexity: O(1) (since board size is fixed at 81 cells).
- Space Complexity: O(1).
def is_valid_sudoku(board):
rows = [set() for _ in range(9)]
cols = [set() for _ in range(9)]
boxes = [set() for _ in range(9)]
for r in range(9):
for c in range(9):
val = board[r][c]
if val == ".": continue
box_idx = (r // 3) * 3 + (c // 3)
if (val in rows[r] or
val in cols[c] or
val in boxes[box_idx]):
return False
rows[r].add(val)
cols[c].add(val)
boxes[box_idx].add(val)
return True