You get a 9 × 9 Sudoku board as a grid of characters. Each cell holds a digit from 1 to 9, or . if it is empty.
Fill every empty cell so that each row, each column and each of the nine 3 × 3 boxes contains every digit from 1 to 9 exactly once. Change the board in place; nothing is returned.
The puzzle is guaranteed to have exactly one solution.
Example 1
Input: board =
. . . | 1 8 . | 9 3 .
. . . | . . . | . . 2
. . 4 | . 6 . | 8 1 .
------+-------+------
. 8 7 | 9 . . | . . .
5 . . | 6 . 3 | 2 . .
4 . . | . . . | . . .
------+-------+------
. 5 . | 4 . 9 | . . .
. . 6 | 5 7 . | 1 . 9
. 4 9 | . 3 . | 7 . 8
Output: board =
6 7 2 | 1 8 5 | 9 3 4
8 1 5 | 3 9 4 | 6 7 2
9 3 4 | 7 6 2 | 8 1 5
------+-------+------
2 8 7 | 9 5 1 | 4 6 3
5 9 1 | 6 4 3 | 2 8 7
4 6 3 | 8 2 7 | 5 9 1
------+-------+------
7 5 8 | 4 1 9 | 3 2 6
3 2 6 | 5 7 8 | 1 4 9
1 4 9 | 2 3 6 | 7 5 8
Why: the only way to complete every row, column and box1 more example and the constraints
Example 2
Input: board =
6 7 2 | 1 8 5 | 9 3 4
8 1 5 | 3 9 4 | 6 7 2
9 3 4 | . . 2 | 8 1 5
------+-------+------
2 8 7 | 9 5 1 | 4 6 3
5 9 1 | . . 3 | 2 8 7
4 6 3 | 8 2 7 | 5 9 1
------+-------+------
7 5 8 | 4 1 9 | 3 2 6
3 2 6 | 5 7 8 | 1 4 9
1 4 9 | 2 3 6 | 7 5 8
Output: board =
6 7 2 | 1 8 5 | 9 3 4
8 1 5 | 3 9 4 | 6 7 2
9 3 4 | 7 6 2 | 8 1 5
------+-------+------
2 8 7 | 9 5 1 | 4 6 3
5 9 1 | 6 4 3 | 2 8 7
4 6 3 | 8 2 7 | 5 9 1
------+-------+------
7 5 8 | 4 1 9 | 3 2 6
3 2 6 | 5 7 8 | 1 4 9
1 4 9 | 2 3 6 | 7 5 8
Why: (2, 4) and (4, 3) must be 6, which forces 7 and 4Constraints
- The board is always 9 rows by 9 columns
- Each cell is a digit from 1 to 9 or a dot
- There is exactly one way to complete the board
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Backtrack in reading orderO(9ᵉ · 27)
- Bitmasks and the most constrained cellO(9ᵉ)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work on a hard 21-given puzzle | Verdict |
|---|---|---|---|
| Backtrack in reading order | O(9ᵉ · 27) | Works, but explores many dead branches | |
| Bitmasks and the most constrained cell | O(9ᵉ) | Best; forced cells first, O(1) checks |
e = number of empty cells, at most 81 minus the givens. Both are exponential in theory; the difference is how much of the tree they visit.
Edge cases to test
- Only one empty cell. Its row already rules out eight digits, so one placement finishes the board.
- A whole row or box empty. Each cell there is forced by its column (or row), so the most constrained cell is always obvious.
- Hard puzzles. Puzzles with about 21 givens need real backtracking; this is where the order of cells matters most.
- Nothing to return. The board is changed in place, so the solved digits must be written into it, not into a copy.
Common mistakes
- Forgetting to reset a cell to
.when every digit failed, which leaves a wrong digit behind for the caller's checks. - Not undoing the mask bits after a failed recursion.
- Continuing the search after a solution is found instead of returning true all the way up, which then undoes the answer.
How to talk through it
- 1Clarify. Is the solution unique? Do I change the board in place? Are the givens always valid?
- 2Start with backtracking. Fill cells in order, try the digits that fit, undo on a dead end.
- 3Make checks cheap. Bitmasks per row, column and box give a cell's free digits in O(1).
- 4Choose the tightest cell. Filling the cell with the fewest options first turns most steps into forced moves and catches dead ends early.
- 5Test out loud. Run a board with one gap, an empty row, and a hard puzzle with about 21 givens.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?