Valid sudoku

Created by mcaylak
mail@pastecode.io avatar
unknown
java
2 years ago
1.1 kB
4
Indexable
Never
class Solution {
   public static boolean isValidSudoku(char[][] board) {

        for (int i = 0;i<board.length;i++){
            HashSet<Character> rowControl = new HashSet<>();
            HashSet<Character> colControl = new HashSet<>();
            for (int j = 0; j < board.length; j++) {
                if(i%3==0&&j%3==0 && !checkMini(i,j,board)){
                    return false;
                }
                if(board[i][j] != '.' && !rowControl.add(board[i][j])){
                    return false;
                }
                if(board[j][i] != '.' && !colControl.add(board[j][i])){
                    return false;
                }

            }
        }


        return true;

    }

    private static boolean checkMini(int row, int col,char[][] board) {
        HashSet<Character> miniSudoku = new HashSet<>();
        for (int i = row; i < row+3; i++) {
            for (int j = col; j < col+3; j++) {
                if(board[i][j] != '.' && !miniSudoku.add(board[i][j])){
                    return false;
                }
            }
        }
        return true;
    }
}