Skip to content
AI360Xpert
Beta
Difficulty: HardBacktracking

Sudoku Solver

Problem in Plain English

Fill a uniquely solvable 9 by 9 Sudoku board in place while preserving its clues. Track used digits in each row, column and box, and undo every failed guess.

Problem Statement

Given a 9 by 9 character matrix board, fill every . with a digit from 1 through 9. Each row, each column and each of the nine 3 by 3 boxes must contain each digit exactly once.

The input has exactly one solution. Modify the same board in place; do not return a replacement board or change any clue. Examples below show the matrix as nine row strings for readability; code receives mutable character rows.

Constraints

  • board.length == 9; every row has length 9.
  • Each cell is a character from 1 through 9 or ..
  • The input has exactly one solution.

Examples

Example 1

Input
board rows = ["53..7....","6..195...",".98....6.","8...6...3","4..8.3..1","7...2...6",".6....28.","...419..5","....8..79"]
Output
board rows = ["534678912","672195348","198342567","859761423","426853791","713924856","961537284","287419635","345286179"]

Every original clue is retained. For example, row 0 becomes 534678912, column 0 becomes 561847923, and the top-left box contains 534, 672, 198: each has all digits exactly once.

Example 2

Input
board rows = ["534678912","672195348","198342567","859761423","426853791","713924856","961537284","287419635","345286179"]
Output
board rows = ["534678912","672195348","198342567","859761423","426853791","713924856","961537284","287419635","345286179"]

An already solved board has zero empty cells. The base case succeeds immediately and leaves every character unchanged.

Example 3

Input
board rows = [".34678912","672195348","198342567","859761423","426853791","713924856","961537284","287419635","345286179"]
Output
board rows = ["534678912","672195348","198342567","859761423","426853791","713924856","961537284","287419635","345286179"]

Only (0,0) is empty. Its row, column and box all require digit 5, so one placement completes the unique solution.

Intuition

Validation checks the clues but does not supply missing digits. Solving requires choosing a legal digit, exploring what it forces later, and retracting it if a later cell has no option. In Example 1, the first empty cell (0,2) allows 1, 2 or 4. A locally legal 1 eventually fails, so all affected guesses must be undone before trying 2 and then the correct 4. Three used-digit masks make each legality check constant time.

Approaches

Backtracking Masks

Optimal

Solution Details

Reveal Backtracking Masks: intuition, complexity, and code

Hints

Hint 1
For an empty cell, eliminate digits already used by its row, column and box.
Hint 2
Store those used digits as three masks; compute the box from integer division by 3.
Hint 3
Try a legal digit, recurse to the next empty cell, and restore all three masks and the dot on failure. Stop undoing when a full solution succeeds.

Edge Cases

  • A solved board needs no placements and must remain unchanged.
  • A single blank requires one legal digit and a direct base-case return.
  • A locally legal digit can fail much later; every nested guess must be undone.
  • No-solution and conflicting-clue inputs are outside the unique-solution contract.

Common Mistakes and Interview Tips

  • Restoring the board while leaving one mask bit set contaminates sibling branches.
  • Using r / 3 + c / 3 without multiplying the row group by 3 merges different boxes.
  • Failing to mask the complement with 511 creates candidates outside digits 1 through 9.
  • Undoing after a successful recursive return erases the solution.

Key Takeaway

Constraint search works by maintaining an exact partial-state invariant: place one legal choice, explore, and reverse every state change if that choice cannot be completed.