Skip to main content
LeetCode 37, Hard. Topics: Array, Hash Table, Backtracking, Matrix. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 13 parametrized pytest cases, and a playground notebook:

Problem

Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules:
  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
The '.' character indicates empty cells.

Examples

Example 1

Constraints

  • board.length == 9
  • board[i].length == 9
  • board[i][j] is a digit or ’.’.
  • It is guaranteed that the input board has only one solution.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Grind.
Last modified on August 25, 2026