Description
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
- Each of the digits
1-9must occur exactly once in each row. - Each of the digits
1-9must occur exactly once in each column. - Each of the digits
1-9must occur exactly once in each of the 93x3sub-boxes of the grid.
The '.' character indicates empty cells.
Example 1:
Input: board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
Output: [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
Constraints:
board.length == 9board[i].length == 9board[i][j]is a digit1-9or'.'.- It is guaranteed that the input board has only one solution.
Brute Force Approach: Standard Backtracking with Grid Scan Validation
Intuition
Scan the 9×9 board cell-by-cell. Whenever an empty cell '.' is encountered:
- Try placing digits
'1'through'9'. - Check if placement is valid by scanning the row, column, and 3×3 sub-box using a loop (O(9) time check).
- Recurse to solve the rest of the board. If a branch fills the board, return
true. - If no digits
'1'-'9'lead to a solution, backtrack (board[i][j] = '.') and returnfalse.
class Solution {
public void solveSudoku(char[][] board) {
backtrack(board);
}
private boolean backtrack(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
for (char c = '1'; c <= '9'; c++) {
if (isValid(board, i, j, c)) {
// 1. CHOOSE
board[i][j] = c;
// 2. EXPLORE
if (backtrack(board)) {
return true;
}
// 3. UN-CHOOSE (Backtrack)
board[i][j] = '.';
}
}
return false; // Triggers backtracking if no digit 1-9 is valid
}
}
}
return true; // All empty spots filled successfully
}
private boolean isValid(char[][] board, int row, int col, char c) {
for (int i = 0; i < 9; i++) {
// Check same row
if (board[row][i] == c) return false;
// Check same column
if (board[i][col] == c) return false;
// Check 3x3 sub-box
if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) return false;
}
return true;
}
}Complexity
- Time Complexity: O(9N) — N is the number of empty cells (up to 81). For each empty cell, up to 9 choices are evaluated with an O(9) validity check.
- Space Complexity: O(N) — Maximum recursion stack depth equals the number of empty cells.
Most Optimized Solution: Backtracking with O(1) State Tracking
Intuition
Instead of running an O(9) loop for isValid() on every digit placement, use three boolean arrays to check availability in O(1) constant time:
rows[r][num]: Tracks if numbernumis present in rowr.cols[c][num]: Tracks if numbernumis present in columnc.boxes[boxIdx][num]: Tracks if numbernumis present in 3×3 boxboxIdx, whereboxIdx = (r / 3) * 3 + (c / 3).
Additionally, move linearly cell-by-cell (r, c) instead of re-scanning the board from (0, 0) at every recursive step.
class Solution {
private boolean[][] rows = new boolean[9][10];
private boolean[][] cols = new boolean[9][10];
private boolean[][] boxes = new boolean[9][10];
public void solveSudoku(char[][] board) {
// Step 1: Pre-fill state arrays with existing digits on the board
for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++) {
if (board[r][c] != '.') {
int num = board[r][c] - '0';
int boxIdx = (r / 3) * 3 + (c / 3);
rows[r][num] = true;
cols[c][num] = true;
boxes[boxIdx][num] = true;
}
}
}
backtrack(board, 0, 0);
}
private boolean backtrack(char[][] board, int r, int c) {
// Base case: Reached past the last row
if (r == 9) return true;
// Compute next cell coordinates
int nextR = (c == 8) ? r + 1 : r;
int nextC = (c == 8) ? 0 : c + 1;
// Skip non-empty cells
if (board[r][c] != '.') {
return backtrack(board, nextR, nextC);
}
int boxIdx = (r / 3) * 3 + (c / 3);
for (int num = 1; num <= 9; num++) {
// Step 2: O(1) state check
if (!rows[r][num] && !cols[c][num] && !boxes[boxIdx][num]) {
// 1. CHOOSE
board[r][c] = (char) (num + '0');
rows[r][num] = true;
cols[c][num] = true;
boxes[boxIdx][num] = true;
// 2. EXPLORE
if (backtrack(board, nextR, nextC)) {
return true;
}
// 3. UN-CHOOSE (Backtrack)
board[r][c] = '.';
rows[r][num] = false;
cols[c][num] = false;
boxes[boxIdx][num] = false;
}
}
return false;
}
}Complexity
- Time Complexity: O(9N) — Significantly faster execution in practice due to O(1) constant-time checks and direct linear traversal without re-scanning solved cells.
- Space Complexity: O(1) — Fixed auxiliary storage for state matrices (9×10) and maximum call stack depth bounded by 81.
Easy Memory Rule
“Try
1-9at empty cells. Validate row, col, and box index(r / 3) * 3 + (c / 3). If valid → place digit →recurse→ backtrack.”
1. Why [9][10] Dimensions?
private boolean[][] rows = new boolean[9][10];
- First Dimension (
9): Represents the 9 rows (or 9 columns / 9 boxes) on the Sudoku board, indexed from0to8. - Second Dimension (
10): Represents the Sudoku digits 1 through 9. - If we created an array of size
9, valid indices would be0through8. To check digit5, we would have to writerows[r][5 - 1]. - By creating an array of size
10, indices range from0to9. We can use the digit directly as the index without subtracting1: rows[r][1]tracks if digit 1 is usedrows[r][9]tracks if digit 9 is used- Index
0is simply left unused.
2. Why the nextR and nextC Condition?
int nextR = (c == 8) ? r + 1 : r;
int nextC = (c == 8) ? 0 : c + 1;
This moves cell-by-cell through the grid in standard reading order (left-to-right, row-by-row):
(0,0) -> (0,1) -> ... -> (0,8)
↓
(1,0) -> (1,1) -> ... -> (1,8)
-
**When
c < 8**(not at the end of the row): -
Move to the right neighbor in the same row:
nextR = r,nextC = c + 1. -
**When
c == 8**(reached the end of the row): -
Wrap around to the start of the next row:
nextR = r + 1,nextC = 0.