Skip to problem
←

Sudoku Solver

Hard 0 attempts

Solve the given 9 × 9 Sudoku board in place.

Each cell is a character: "1" through "9" is a fixed clue, and "." is an empty cell. Replace only empty cells so that:

  • Each row contains every digit from 1 through 9 exactly once.
  • Each column contains every digit exactly once.
  • Each aligned 3 × 3 box contains every digit exactly once.

Keep the original clues unchanged and write digit strings, not numbers. The puzzle is valid and has exactly one solution. An already completed board should remain unchanged.

Function contract

Implement solveSudoku(board).

Output: Fill "." cells in the original board with digit characters "1" through "9". Keep every given clue unchanged. The return value is ignored.

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"]]

Example 2

Input
board = [["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"]]
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"]]
Visual Aid
The nine 3 × 3 boxes
537619598686348317266284195879

Thicker lines separate the boxes. Keep the shown clues and fill the blank cells; row, column, and box rules all apply.

Constraints
  • board is a 9 × 9 grid of characters 1 through 9 or dot.
  • The given clues are valid and the puzzle has exactly one solution.

Use backtracking to fill empty cells one by one. For each cell, try digits 1–9 and validate against Sudoku row, column, and 3×3 box constraints. If no digit works, backtrack.

javascript
function solveSudoku(board) {
  function isValid(row, col, ch) {
    const boxRow = Math.floor(row / 3) * 3;
    const boxCol = Math.floor(col / 3) * 3;
    for (let i = 0; i < 9; i++) {
      if (board[row][i] === ch) return false;
      if (board[i][col] === ch) return false;
      if (board[boxRow + Math.floor(i / 3)][boxCol + (i % 3)] === ch) return false;
    }
    return true;
  }

  function solve() {
    for (let r = 0; r < 9; r++) {
      for (let c = 0; c < 9; c++) {
        if (board[r][c] !== '.') continue;
        for (let d = 1; d <= 9; d++) {
          const ch = String(d);
          if (isValid(r, c, ch)) {
            board[r][c] = ch;
            if (solve()) return true;
            board[r][c] = '.';
          }
        }
        return false;
      }
    }
    return true;
  }

  solve();
}

Time: O(9^(empty cells)) — bounded by the fixed board size Space: O(81) recursion depth

Loading discussion…

Saved in this browser only. Private to you.

JavaScript