Skip to content

Sudoku Solver

HardBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

Write a function that solves a Sudoku puzzle by filling the board in place. The board is a 9 by 9 list of lists of single-character strings: digits "1" to "9", and "." for an empty cell. When you finish:

  • each row, each column, and each of the nine 3 by 3 boxes contains the digits 1 to 9 exactly once, and
  • every digit that was given at the start is unchanged.

The function is solve_sudoku(board) (Python) / solveSudoku(board) (TypeScript). It returns nothing; the tests read the board after the call.

before (row 0):  ["5", "3", ".", ".", "7", ".", ".", ".", "."]
after  (row 0):  ["5", "3", "4", "6", "7", "8", "9", "1", "2"]

Constraints

  • board.length == 9 and board[i].length == 9
  • board[i][j] is a digit 1 to 9 or '.'.
  • The puzzle has exactly one solution.