public class Solution {
public boolean isValidSudoku(char[][] board) {
if (board == null || board.length == 0 || board[0].length == 0) {
return false;
}
int row = board.length;
int col = board[0].length;
if (row != col) {
return false;
}
// row
for (int i = 0; i < row; i++) {
Set<Character> set = new HashSet<Character>();
for (int j = 0; j < col; j++) {
if (set.contains(board[i][j])) {
return false;
} else if (board[i][j] != '.') {
set.add(board[i][j]);
}
}
}
// colummn
for (int j = 0; j < col; j++) {
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < row; i++) {
if (set.contains(board[i][j])) {
return false;
} else if (board[i][j] != '.') {
set.add(board[i][j]);
}
}
}
// matrix square
for (int m = 0; m < 9; m++) {
Set<Character> set = new HashSet<Character>();
for (int i = (m / 3) * 3; i < (m / 3) * 3 + 3; i++) {
for (int j = (m % 3) * 3; j < (m % 3) * 3 + 3; j++) {
if (set.contains(board[i][j])) {
return false;
} else if (board[i][j] != '.') {
set.add(board[i][j]);
}
}
}
}
return true;
}
}