15% off all plans & roadmaps Use code
See plansLeetCode 37 · Solution & editorial
Write a program to solve a Sudoku puzzle by filling the empty cells.
Sudoku Solver is a Backtracking Patterns problem, in the N-Queens / Constraint Satisfaction subpattern. The pattern covers subsets, permutations, combinations, N-Queens, and word search
Solve N-Queens and Sudoku using backtracking with constraint propagation.
The reference solution runs in O(9^(n)) time and O(1) space.
Sudoku Solver has been reported in interviews at DoorDash, Amazon, Oracle, Pinterest, Facebook, Intuit.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Sudoku Solver is a Backtracking Patterns problem, in the N-Queens / Constraint Satisfaction subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.