Sudoku Solver
Fill the empty cells of a 9×9 Sudoku so each row, column, and 3×3 box holds digits 1–9 exactly once.
Open on LeetCode ↗Intuition
Take the first empty cell, try each digit that doesn't clash with its row, column, or box, and recurse. A cell with no legal digit proves an earlier guess wrong — undo and try the next. Constraint checks kill branches so fast the 9^81 space collapses.
Approach
Scan for the next blank
Solve cells in reading order; the recursion's state is just 'first blank from here'.
Try digits with a validity check
A digit is legal if absent from the cell's row, column, and 3×3 box (box index = (r//3)*3 + c//3).
Recurse, undo on failure
Place, recurse; if the sub-board is unsolvable, erase and continue. True bubbles up when no blanks remain.
Solution & live demo
Edge cases
No blank found → immediately true.
Cells with a single legal digit fall out instantly, cascading constraints — that's why real puzzles solve in milliseconds.