Advertisement

Sudoku Solver - LeetCode 37 Solution

Sudoku Solver - Complete Solution Guide

Sudoku Solver is LeetCode problem 37, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. The '.' character indicates empty cells. Example 1: Input: board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",

Detailed Explanation

The Sudoku Solver problem requires you to write a program that solves a given Sudoku puzzle. The input is a 9x9 grid represented as a 2D array of characters, where digits '1'-'9' represent filled cells and '.' represents empty cells. The task is to fill the empty cells such that the solution satisfies the Sudoku rules: each row, each column, and each of the nine 3x3 sub-boxes must contain the digits '1'-'9' exactly once. The provided board is guaranteed to have only one solution, and the solution must be done in-place, modifying the given board directly.

Solution Approach

The provided solutions use a backtracking algorithm to explore possible solutions for the Sudoku puzzle. The algorithm iterates through empty cells, attempting to fill them with digits from 1 to 9. Before placing a digit, it checks if the digit is valid according to Sudoku rules (not already present in the row, column, or 3x3 box). If the digit is valid, it's placed, and the algorithm recursively calls itself to solve the rest of the puzzle. If the recursive call leads to a solution, the algorithm returns true. Otherwise, it backtracks by removing the digit and trying the next one. The sets (or boolean arrays) are used to track the digits already used in each row, column, and box, to quickly check the validity.

Step-by-Step Algorithm

  1. Step 1: Initialize sets (or boolean arrays) to keep track of the numbers present in each row, column, and 3x3 box.
  2. Step 2: Find all empty cells in the board and store their coordinates.
  3. Step 3: Define a recursive backtracking function that takes the index of the current empty cell to fill as input.
  4. Step 4: The base case for the recursion is when all empty cells have been filled. If this occurs, return true.
  5. Step 5: For each empty cell, iterate through the numbers 1 to 9 and check if placing the number in the cell is valid (i.e., doesn't violate Sudoku rules).
  6. Step 6: If a number is valid, place it in the cell, update the row, column, and box sets, and recursively call the backtracking function for the next empty cell.
  7. Step 7: If the recursive call returns true (meaning a solution was found), return true from the current call.
  8. Step 8: If the recursive call returns false (meaning the current number led to a dead end), remove the number from the cell, update the row, column, and box sets to reflect the removal, and try the next number.
  9. Step 9: If none of the numbers 1 to 9 lead to a solution for the current empty cell, return false, indicating that the current path is invalid.
  10. Step 10: The main function initiates the backtracking process by calling the recursive function with the index of the first empty cell.

Key Insights

  • Insight 1: Backtracking is essential because we need to explore different possibilities for filling empty cells, and if a choice leads to an invalid state, we must revert (backtrack) and try another possibility.
  • Insight 2: Using sets (or boolean arrays in C) to track the digits already present in each row, column, and 3x3 box allows for efficient checking of the Sudoku constraints.
  • Insight 3: Optimizing the search by considering the constraints early (checking if a number is already in the row, column, or box) significantly reduces the search space and improves performance.

Complexity Analysis

Time Complexity: O(9^m)

Space Complexity: O(1)

Topics

This problem involves: Array, Hash Table, Backtracking, Matrix.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Cadence, Citadel, Confluent, DoorDash, Intuit, LINE, Meta, Microsoft, Oracle, Pinterest, Riot Games, Snap, Uber, Yahoo.