Check if Valid Sudoku Blocks in Java


The challenge of solving valid Sudoku blocks

Determine if a 9×9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

(A partially filled sudoku which is valid.)

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Example 1:

Input:
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
Output: true

Example 2:

Input:
[
  ["8","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
Output: false

Explanation: Same as Example 1, except with the 5 in the top left corner being 
    modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

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.
  • The given board contain only digits 1-9 and the character '.'.
  • The given board size is always 9x9.

The solution written in Java

class Solution {
    
    // Store our blocks
    Set blockSet = new HashSet<>();
    
    // Perform the check
    public boolean isValidSudoku(char[][] board) {
        // Row and Column variables to check against
        Set rowset = new HashSet<>();
        Set colset = new HashSet<>();

        // Loop through the column
        for(int i = 0; i<9; i++){
            // Loop through the row
            for(int j = 0; j<9; j++){
                // False if exists in column
                if(colset.contains(board[i][j]))
                    return false;
                // False if exists in row
                if(rowset.contains(board[j][i]))
                    return false;
                
                // Add to column and row
                if(board[i][j] != '.') colset.add(board[i][j]);
                if(board[j][i] != '.') rowset.add(board[j][i]);
            }
            // Clear this run at the end
            rowset.clear();
            colset.clear();
        }
        
        // While we loop the board
        int i = 0, j = 0;
        while(i < 9 && j < 9) {
        	while(i < 9) {
                // False if not valid
        		if(!isValid(board, i, j))
                    return false;
                // Increment one section
        		i += 3;
                // Clear the board
        		blockSet.clear();
        	}
            // Next column/row
        	i = 0;
        	j += 3;
        }
        
        // Is valid!
        return true;
    }
    
    // Check if a section is valid
    private boolean isValid(char[][] board, int i, int j) {
        // Loop the column
        for(int x = i; x < i+3; x++) {
            // Loop the row
            for(int y = j; y < j+3; y++) {
                // False if already in the set
                if(blockSet.contains(board[x][y]))
                    return false;
                // If still in a section, add it
                if(board[x][y] != '.')
                    blockSet.add(board[x][y]);
            }
        }
        
        // Is valid!
        return true;
    }
}