Problem: Valid Sudoku
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
//Each row must contain the digits 1-9 without repetition. //Each column must contain the digits 1-9 without repetition. //Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
Note:
A Sudoku board (partially filled) could be valid but is not necessarily solvable. Only the filled cells need to be validated according to the mentioned rules.
class Solution {
public boolean isValid(char[][] board, int row,int col,char val){
for(int i=0;i<9;i++){
if(board[row][i] == val) return false;
if(board[i][col] == val) return false;
if(board[3*(row/3)+i/3][3*(col/3)+i%3] == val) return false;
}
return true;
}
public boolean isValidSudoku(char[][] board) {
boolean res = true;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j] != '.'){
if(board[i][j] != '.'){
char val = board[i][j];
board[i][j] = '.';
res = isValid(board,i,j,val);
board[i][j] = val;
}
if(!res) return res;
}
}
}
return true;
}
}