Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Sudoku Solver Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Sudoku Solver

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

You are given a 9 X 9 sudoku board where some cells are filled. You need to solve the sudoku.

A correctly solved sudoku has the following properties:

  • Each row must contain all integers from 1 to 9.
  • Each column must contain all integers from 1 to 9.
  • Each of the nine 3 X 3 sub-boxes must contain all integers from 1 to 9.

Naive Approach

The naive approach is to fill all the empty cells with all the possible values from 0 - 9 and check if the final configuration of the board is valid or not. If it is valid, we return, else we go over to the next configuration.

Analysis

  • Time Complexity: O(9(n * n))
  • Space Complexity: O(n * n)

Optimal Approach

The optimal approach will be the same as the naive approach, but we will cut off many possible impossible configurations as soon as we find them to occur, instead of making the entire configuration and checking it at the end. Before assigning a number, we check if it is safe to assign.  Check, if the current number is not present in the current row, current column, and current 3 X 3 subgrid, only then assign that number, else backtrack to the previous state.

Analysis

  • Time Complexity: O(9(n * n)) // The worst case is the same but the average case will be much better
  • Space Complexity: O(n * n)

Implementation

C++
bool isValid(int value, int row, int col, vector<vector<char>> &board){
	bool rowIsValid = true;
	bool colIsValid = true;
	char digit = to_string(value).back();
	for(int i = 0; i < 9; i++){
		if(digit == board[row][i]){
			rowIsValid = false;
			break;
		}
		if(digit == board[i][col]){
			colIsValid = false;
			break;
		}
	}
	if(rowIsValid == false || colIsValid == false){
		return false;
	}
	int subgridrow = row / 3;
	int subgridcol = col / 3;
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			int currRow = subgridrow * 3 + i;
			int currCol = subgridcol * 3 + j;
			if(digit == board[currRow][currCol]){
				return false;
			}
		}
	}
	return true;
}
bool solve(int row, int col, vector<vector<char>> &board){
	if(col == board.size()){
		if(row == board.size() - 1){
			return true;
		}
		row++;
		col = 0;
	}
	if(board[row][col] != '.'){
		return solve(row, col + 1, board);
	}
	for(int i = 1; i < 10; i++){
		if(isValid(i, row, col, board)){
			board[row][col] = to_string(i).back();
			if(solve(row, col + 1, board)){
				return true;
			}
		}
	}
	board[row][col] = '.';
	return false;
}
void sudokuSolver(vector<vector<char>> &sudoku) {
	solve(0, 0, sudoku);
}
Java
class Solution {
	boolean isValid(int value, int row, int col, char[][] board){
		boolean rowIsValid = true;
		boolean colIsValid = true;
        char digit = Character.forDigit(value, 10);
		for(int i = 0; i < 9; i++){
			if(digit == board[row][i]){
				rowIsValid = false;
				break;
			}
			if(digit == board[i][col]){
				colIsValid = false;
				break;
			}
		}
		if(rowIsValid == false || colIsValid == false){
			return false;
		}
		int subgridrow = row / 3;
		int subgridcol = col / 3;
		for(int i = 0; i < 3; i++){
			for(int j = 0; j < 3; j++){
				int currRow = subgridrow * 3 + i;
				int currCol = subgridcol * 3 + j;
				if(digit == board[currRow][currCol]){
					return false;
				}
			}
		}
		return true;
	}
	boolean solve(int row, int col, char[][] board){
		if(col == board.length){
			if(row == board.length - 1){
				return true;
			}
			row++;
			col = 0;
		}
		if(board[row][col] != '.'){
			return solve(row, col + 1, board);
		}
		for(int i = 1; i < 10; i++){
			if(isValid(i, row, col, board)){
				board[row][col] = Character.forDigit(i, 10);
				if(solve(row, col + 1, board)){
					return true;
				}
			}
		}
		board[row][col] = '.';
		return false;
	}
	void sudokuSolver(char[][] sudoku) {
	    solve(0, 0, sudoku);
	}
}
Related Content
Combination Sum 1
Combination Sum 2
Combinations
Consecutively Descending Integers
Generate Parentheses
Kth Permutation Sequence
Letter Combinations of a Phone Number
N Queens Problem
Palindrome Partitioning
Rat In A Maze
Restore IP Addresses
String Permutations
Subset Sum
Subsets
Subsets - II
Tug of War
Word Break
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet