Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
My Solution :
import java.util.HashSet;
class Solution {
public boolean isValidSudoku(char[][] board) {
for (int i = 0; i < 9; i ++) {
HashSet<Character> set = new HashSet<>();
for (int j = 0; j < 9; j ++) {
if (board[i][j] != '.' && !set.add(board[i][j])) {
System.out.println(1);
return false;
}
}
}
for (int j = 0; j < 9; j ++) {
HashSet<Character> set = new HashSet<>();
for (int i = 0; i < 9; i ++) {
//System.out.println(i + " " + j);
if (board[i][j] != '.' && !set.add(board[i][j])) {
System.out.println(2);
return false;
}
}
}
int xbegin ;
int ybegin;
for (int x = 0; x < 3; x ++) {
for (int y = 0; y < 3; y++) {
xbegin = x * 3;
ybegin = y * 3;
HashSet<Character> set = new HashSet<>();
for (int i = xbegin; i < xbegin + 3; i ++) {
for (int j = ybegin; j < ybegin + 3; j ++) {
//System.out.println(i + " " + j);
if (board[i][j] != '.' && !set.add(board[i][j])) {
System.out.println(3);
System.out.println(i + " " + j);
return false;
}
}
}
}
}
return true;
}
}