2Brute2Best
πŸ”₯0

Sudoku Solver

HardΒ·LeetCode #37 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given a 9 Γ— 9 Sudoku board. Digits 1 to 9 are clues. A . marks an empty cell.

Fill in every empty cell so that each row, each column and each of the nine 3 Γ— 3 boxes holds the digits 1 to 9 exactly once. Change the board in place. The function returns nothing.

The puzzle has exactly one solution.

Input

board: a 9 Γ— 9 grid of characters. Each one is a digit 1 to 9 or ..

Output

The same grid, filled in. Nothing is returned.

Example 1
Input: board = the 9 Γ— 9 grid from LeetCode's example 1. Its first row is 5 3 . . 7 . . . .
Output: The same grid, filled in. Its first row is 5 3 4 6 7 8 9 1 2.

The puzzle has one solution. The blanks in the first row get 4, 6, 8, 9, 1 and 2.

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.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œfill in the empty cells”
β†’ A search over choices: each empty cell takes one digit, and a wrong digit must be undone.
β€œeach row, column and box holds 1 to 9”
β†’ Three overlapping groups of nine cells. Which digits each group already holds is the state, so bit masks fit.
β€œexactly one solution”
β†’ The first full board is the answer, so each caller must stop as soon as the recursion succeeds.
03

Approach Complexity Ladder

Brute Force β€” fill in order, check by scanningO(9^E)O(1)

Go through the empty cells in order; each try scans its row, column and box (27 reads)

Better β€” bit masks for O(1) checksO(9^E)O(1)

The same cell order as the brute force, but each digit check is one AND on three bit masks

Optimal β€” bit masks plus the fewest-options cell first (MRV)O(9^E)O(1)

Bit masks for O(1) checks, and always fill the empty cell with the fewest digits that fit

Brute Force Intuition

Walk the empty cells in row-by-row order. For each one, try the digits 1 to 9. A digit is allowed when the row, the column and the box do not already hold it, which is found by scanning them. If a cell has no digit that fits, go back to the cell before it. The first full board is the answer.

Recognition rule for this sub-pattern:

Place items one by one, checking the rules before each placement. Prune as early as possible.
Remember this Β· Backtracking
Trigger
"All subsets / permutations / combinations", place items under rules (N-Queens, Sudoku), find a path in a grid.
Think
At each step, what are my choices, and how do I undo a choice?
Key question
What is my state, when is it complete, and when do I prune?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Backtracking translate into production architectures and low-level memory mechanics.