#37

Sudoku Solver

Hard Backtracking on empty cells

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Fill the empty cells of a 9×9 Sudoku so every row, column and 3×3 box contains 1–9 exactly once. The puzzle has exactly one solution.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: fill the board so every row, column and box has 1–9.

Why this works: try each allowed digit in an empty cell, recurse, and undo when a later cell has no options.

Example: an empty cell where row, column and box already use 1–8

only 9 is allowed → place 9 and move ona dead end later → remove 9 and try the previous cell's next digit

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Not resetting the cell to '.' after a failed try.

Solve on LeetCode ↗
Complexity
  • Time: Exponential in the worst case, fast in practice with O(1) checks.
  • Space: O(81).
Topic: Recursion & Backtracking
🎬 Episodes 29–36: video coming soon