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.
java:
public class Solution {
public boolean isValidSudoku(char[][] board) {
for (int i = 0;i < 9;i++){
HashSet<Character> table = new HashSet<Character>();
for (int j = 0;j<9;j++){
if (board[i][j]=='.'){
continue;
}
if (table.contains(board[i][j])) return false;
table.add(board[i][j]);}
}
for (int j = 0;j < 9;j++){
HashSet<Character> table = new HashSet<Character>();
for (int i = 0;i<9;i++){
if (board[i][j]=='.'){
continue;
}
if (table.contains(board[i][j])) return false;
table.add(board[i][j]);}
}
for (int i = 0; i< 3;i ++){
for (int j = 0;j < 3;j++){
HashSet<Character> table = new HashSet<Character>();
for (int m = i*3;m < i*3+3;m++){
for (int n = j*3;n < j*3+3;n++){
if (board[m][n]=='.'){
continue;
}
if (table.contains(board[m][n])) return false;
table.add(board[m][n]);}
}
}
}
return true;
}
}