Question
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
import java.util.Hashtable;
public class Solution {
public boolean isValidSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
Hashtable<Character, Character> ht = new Hashtable<Character, Character>();
for (int j = 0; j < 9; j++) {
char c = board[i][j];
if (c=='.') {
}else{
if (ht.get(c)==null) {
ht.put(c, c);
}else {
return false;
}
}
}
}
for (int i = 0; i < 9; ++i) {
Hashtable<Character, Character> ht = new Hashtable<Character, Character>();
for (int j = 0; j < 9; ++j) {
char c = board[j][i];
if (c=='.') {
}else{
if (ht.get(c)==null) {
ht.put(c, c);
}else {
return false;
}
}
}
}
return isSmallValid(board, 0, 0) && isSmallValid(board, 3, 0)
&& isSmallValid(board, 6, 0) && isSmallValid(board, 0, 3)
&& isSmallValid(board, 3, 3) && isSmallValid(board, 6, 3)
&& isSmallValid(board, 0, 6) && isSmallValid(board, 3, 6)
&& isSmallValid(board, 6, 6);
}
private boolean isSmallValid(char[][] board, int x, int y) {
Hashtable<Character, Character> ht = new Hashtable<Character, Character>();
for (int i = x; i < 3 + x; ++i) {
for (int j = y; j < 3 + y; ++j) {
char c = board[i][j];
if (c=='.') {
}else{
if (ht.get(c)==null) {
ht.put(c, c);
}else {
return false;
}
}
}
}
return true;
}
}