Given a 9 × 9 board with some cells filled with digits 1–9 and the rest empty (.), return whether it is valid so far: no digit repeats within a row, a column or one of the nine 3 × 3 boxes. You don't have to solve the puzzle.
Before you code
Every filled cell belongs to exactly one row, one column and one box. If you can compute the box number from (r, c) without a loop, a single pass over the 81 cells is enough.
Hints
- The box number is
(r // 3) * 3 + c // 3, from 0 at the top left to 8 at the bottom right. - Keep 9 sets for rows, 9 for columns and 9 for boxes, or 27 arrays of 9 booleans.
- For each filled cell, check its three sets; if the digit is already in any of them, the board is invalid. Otherwise add it to all three.
Production & complexity notes
When solving Valid Sudoku on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Index arithmetic to reinforce the underlying pattern.
What it practices
Turning a position into a group number with integer division, from Index arithmetic.