visual walkthrough
Valid Sudoku
Encoding row, column and box membership in sets; clean hashing of 2D data.
The idea
A board is valid when no row, no column and no 3×3 box contains the same digit twice. You don't need to solve it, only to look for repeats.
Giving every row, column and box its own set lets one pass over the grid catch a repeat the moment it appears.
Complexity
| approach | time | space |
|---|---|---|
| Check each group | O(81 × 3) | O(9) |
| One pass, three sets | O(81) | O(81) |