Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valid sudoku #287

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
55 changes: 55 additions & 0 deletions Hacktoberfest2022/ValidSudoku.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.*;

public class ValidSudoku{

public static void main(String []args){
char [][]sudoku=new char[][]{{'8','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'}};
System.out.println(isValidSudoku(sudoku));
}

//using bitmasking
public static boolean isValidSudoku(char[][] board) {
//arrays for marking numbers as visited for row,column,block
int brow[]=new int[9];
int bcol[]=new int[9];
int bblock[]=new int[9];
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
//if empty slot is encountered continue
if(board[i][j]=='.'){
continue;
}
int val=(int)Math.pow(2,board[i][j]-'0');
//check if the value is already encountered in the row
if((brow[i]&val)>0){
return false;
}
//if not encountered, mark it as encountered
brow[i]+=val;
//check if the value is already encountered in the column
if((bcol[j]&val)>0){
return false;
}
//if not encountered, mark it as encountered
bcol[j]+=val;
int mod = (3 * (i / 3)) + (j / 3);
//check if the value is already encountered in the block
if((bblock[mod]&val)>0){
return false;
}
//if not encountered, mark it as encountered
bblock[mod]+=val;
}
}
return true;
}

}