0% found this document useful (0 votes)
14 views1 page

Tree Solution

This method checks if the given sudoku board represented as a 2D character array is valid. It does this by counting the occurrences of each number 1-9 in each row, column and sub-box of the board and returns false if any number occurs more than once.

Uploaded by

adfadfad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Tree Solution

This method checks if the given sudoku board represented as a 2D character array is valid. It does this by counting the occurrences of each number 1-9 in each row, column and sub-box of the board and returns false if any number occurs more than once.

Uploaded by

adfadfad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

public static boolean isValidSudoku_array(char[][] board) {

int[][] row = new int[9][9];


int[][] col = new int[9][9];
int[][][] subBox = new int[3][3][9];

for (int i = 0; i < 9; i++) {


for (int j = 0; j < 9; j++) {
char ch = board[i][j];
if (ch != '.') {
int num = ch - '0' - 1;
row[i][num]++;
col[j][num]++;
subBox[i / 3][j / 3][num]++;
if (row[i][num] > 1 || col[j][num] > 1 || subBox[i / 3][j / 3]
[num] > 1) {
return false;
}
}
}
}

return true;
}

You might also like