AlgoInsight

DSA problem

Valid Sudoku

MediumMatrix (2D Array)Pattern: Matrix Manipulationtop300AmazonMicrosoftAppleUberGoogle

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, and each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Note that an empty cell is represented by the character . and only the cells that are filled with a digit are checked. A board can be valid even if it is not solvable; the task is purely to confirm that no rule is currently violated by the existing digits.

A naive solution would scan each of the nine rows, then each of the nine columns, then each of the nine boxes separately, requiring three passes. A cleaner and more efficient approach validates all three constraints in a single pass over the 81 cells.

The intuition for the optimal approach is to maintain three groups of seen-digit sets: one set per row, one per column, and one per 3 x 3 box. As you visit each filled cell, compute its box index as (row / 3) * 3 + (col / 3), and check whether its digit already appears in the corresponding row set, column set, or box set. If any membership test fails the board is invalid; otherwise insert the digit into all three sets and continue. This validates the entire board in one sweep.

Examples

Example 1
Input: board = [["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
Explanation: No row, column, or 3x3 sub-box contains a repeated digit, so the board satisfies all three Sudoku rules and is valid.
Example 2
Input: board = [["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: This board is the same as the first except the top-left cell is 8 instead of 5. Now there are two 8s in the top-left 3x3 sub-box and two 8s in column 0, violating the rules.
Example 3
Input: board = [["1","2",".",".",".",".",".",".","."],["2",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]]
Output: false
Explanation: The digit 2 appears at (0,1) and (1,0), which places two 2s inside the same top-left 3x3 sub-box, so the board is invalid.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

matrixarrayhash-tablesimulation

Browse all DSA problems