Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 43 additions & 61 deletions src/codingquestions/leetcode/SudokuSolver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package codingquestions.leetcode;

/**
/*
37. Sudoku Solver
Solved
Hard
Expand Down Expand Up @@ -34,75 +32,59 @@
board[i].length == 9
board[i][j] is a digit or '.'.
It is guaranteed that the input board has only one solution.
*/
class Solution {
public:
char c[9][9];

bool canBePlaced(char cc, int row, int col){
//check the same row(
for(int i = 0; i < 9; i++){
if(c[row][i] == cc) return false;
}

**/


public class SudokuSolver {
private char[][] board;
public void solveSudoku(char[][] board) {
this.board=board;
helper(0,0);
}
public boolean helper(int row,int col){
//if I'm at last column then move to next row
if(col==9){
row+=1;
col=0;
//same col
for(int i = 0; i < 9; i++){
if(c[i][col] == cc) return false;
}
// if my row is last then
if(row==9) return true;

//if we have a value at the block then we move to the next block
if(board[row][col]!='.') return helper(row,col+1);

// here we will check if the point where we are adding the value is it valid or not from 1 to 9, if its not valid we move to next number, if its valid we add that number and go to the next column; if the helper return false then we revert back to '.';
for(char i='1';i<='9';i++){
if(!isvalid(row,col,i)) continue;
board[row][col]=i;
if(helper(row,col+1)==true) return true;
board[row][col]='.';

//same square
int startRow = ((int)(row / 3)) * 3, startCol = ((int)(col / 3) * 3);
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(c[startRow + i][startCol + j] == cc) return false;
}
}
return false;
return true;
}

public boolean isvalid(int row, int col, char cur){
//check is element exists in the current row or the current column
for(int i=0;i<9;i++){
if(board[row][i]==cur) return false;
if(board[i][col]==cur) return false;
bool solveHelper(int row, int col){
if(col == 9){
row++;
col = 0;
}
if(row == 9) return true;

if(c[row][col] != '.') return solveHelper(row, col + 1);

//create the row border and column border to find the start and end point of the column
int[] rowborder = findSE(row);
int[] colborder = findSE(col);
for(char num = '1'; num <= '9'; num++){
if(canBePlaced(num, row, col)){
c[row][col] = num;
if(solveHelper(row, col + 1)) return true;

//check the values in the current box
for(int i=rowborder[0];i<=rowborder[1];i++) {
for(int j=colborder[0];j<=colborder[1];j++){
if(board[i][j]==cur){
return false;
}
c[row][col] = '.';
}
}
return true;

return false;
}

// to find the start and the end border
private int[] findSE(int coor){
int[] res = new int[2];
if(coor<3){
res[1]=2;
}
else if(coor<6){
res[0]=3;
res[1]=5;
}
else{
res[0]=6;
res[1]=8;
void solveSudoku(vector<vector<char>>& board) {
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++) c[i][j] = board[i][j];

solveHelper(0, 0);
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++) board[i][j] = c[i][j];
}
return res;
}
}
};