Valid Sudoku – Solution & Complexity
Solution Walkthrough
Step 1: Check each rule directly
A straightforward approach is to validate every row, every column, and every
3 x 3box separately. For each group, collect the non-zero digits you see and fail if a digit appears twice. This works well, but it conceptually scans the board multiple times even though the board is only9 x 9.Step 2: Single-pass tracking with row, column, and box state
Iterate through the board once. Ignore
0because it means the cell is empty. For each filled cell, compute its box index with(row // 3) * 3 + (col // 3), then check whether that digit has already appeared in the current row, column, or box. If so, returnfalseimmediately; otherwise mark it as seen and continue. After all 81 cells are processed without a conflict, returntrue.