public boolean isValidSudoku(char[][] board) {
//check the column
for (int i=0; i<board[0].length; i++) {
boolean[] dupCheck = new boolean[256];
for (int j=0; j<board.length; j++) {
if (board[j][i] != '.') {
if (dupCheck[board[j][i]] == true) return false;
else dupCheck[board[j][i]] = true;
}
}
}
//check the row
for (int i=0; i<board.length; i++) {
boolean[] dupCheck = new boolean[256];
for (int j=0; j<board[0].length; j++) {
if (board[i][j] != '.') {
if (dupCheck[board[i][j]] == true) return false;
else dupCheck[board[i][j]] = true;
}
}
}
//check the subBox
for (int i=0; i <board.length/3; i++) {
for (int j=0; j <board[0].length/3; j++) {
boolean[] dupCheck = new boolean[256];
for (int m=i*3; m<i*3+3; m++) {
for (int n=j*3; n<j*3+3; n++) {
if (board[m][n] != '.') {
if (dupCheck[board[m][n]] == true) return false;
else dupCheck[board[m][n]] = true;
}
}
}
}
}
return true;
}
No comments:
Post a Comment