Given a solution of Sudoku puzzle, the task is to check whether the given sudoku solution is valid or not. A valid solution requires that each row, column, and 3×3 sub-matrix must contain the digits 1-9 without repetition.
In our previous article, we discussed how to check whether a Sudoku board configuration is valid. And for this problem, the approach remains the same. However, while that article focused on validating configurations, here we will be validating a complete Sudoku solution.
Using Array of Fixed Length
To check whether a given sudoku solution is valid or not. We have to make sure that each number (1-9) appears only once per row, column, and 3x3 sub-matrix. We can use array or hash table for keeping track of seen/visited elements, Here we have used auxiliary arrays to track the seen/visited of each number in rows, columns, and sub-matrix.
Step-by-step approach:
We use three 2D arrays (rows, cols, subMat) to track which numbers have already appeared in each row, column, and sub-matrix, respectively.
For each cell, we skip if it contains 0 (empty). Otherwise, we check if the number has already appeared in the corresponding row, column, or sub-matrix. If so, the matrix is invalid.
The sub-matrix index is calculated as (i / 3) * 3 + j / 3 where i and j are the row and column indices.
If the current number is not visited before in the current row, column, or sub-matrix then marked as seen/visited.
Output
true
Time Complexity: O(n^2), where n is the size of the Sudoku matrix (i.e, 9). This is because we iterate through all n*n cells of the matrix. Auxiliary Space: O(n^2), due to the three 2D arrays (rows, cols, subMat) of size n*n
Using Bitmasking
In the previous approach, we used arrays to track which numbers have appeared in each row, column, and sub-matrix. Now, we can make this more efficient by using bit manipulation, we represent the presence of numbers with bits in an integer, which reduces memory usage and improves time complexity as well. The implementation steps remain the same as above approach, apart from checking and marking visited values in row, column and sub-matrix.
Step-by-step approach:
Use a single integer for each row, column, and sub-matrix. Each bit in the integer corresponds to a number.
For each cell, we can use bitwise AND (&) to check if a number's bit is already set. If it is, that means the number has appeared before and the Sudoku is invalid.
If the current cell value (i,e, mat[i][j]) is not set in the row, column, or sub-matrix then we have to set the bit for the number using bitwise OR operation (|=) to mark it visited.
Output
true
Time Complexity: O(n^2), where n is the size of the Sudoku matrix (i.e, 9). This is because we iterate through all n*n cells of the matrix. Auxiliary Space: O(n), as we use three arrays (rows, cols, subMat), each of size n to track the seen numbers for rows, columns, and sub-matrix. Each array element holds an integer, which is used for bitwise operations.
For more details and explanations about the solutions, please refer to our previous article.