Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
1-9 without repetition.1-9 without repetition.3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.Note:
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
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: 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.
Constraints:
board.length == 9board[i].length == 9board[i][j] is a digit 1-9 or '.'.Problem Overview: Given a partially filled 9x9 Sudoku board, determine whether it follows Sudoku rules. Each row, column, and 3x3 sub-box must contain digits 1–9 without duplicates. Empty cells are represented by . and should be ignored during validation.
Approach 1: Hash Set Validation (Time: O(81), Space: O(81))
The most straightforward method scans the board once while tracking seen numbers for rows, columns, and 3x3 boxes using hash sets. For each cell, skip .. Otherwise check whether the number already exists in the corresponding row set, column set, or box set. If any lookup returns true, the board is invalid. Otherwise insert the number into all three structures.
This works because hash table lookups run in O(1) average time. You typically maintain three collections: rows[9], cols[9], and boxes[9]. The box index is computed using (r / 3) * 3 + (c / 3). Since the board size is fixed (9×9), the algorithm processes at most 81 cells, giving O(81) time and O(81) auxiliary space. This approach is easy to implement and commonly used in interviews when working with arrays and grid-style problems.
Approach 2: Bit Manipulation (Time: O(81), Space: O(1))
Bit manipulation compresses the same validation logic into integer bitmasks. Instead of hash sets, use three arrays of integers to represent rows, columns, and boxes. Each digit 1–9 maps to a bit position. When reading a number, compute mask = 1 << digit and check whether that bit already exists in the corresponding row, column, or box mask using a bitwise AND.
If the bit is already set, the board violates Sudoku rules. Otherwise update the mask using a bitwise OR. This approach removes the overhead of hash structures and keeps memory constant. It still iterates through the board once, so the time complexity remains O(81), but the space complexity drops to O(1) since only fixed-size integers are used. Bitmask techniques are common when solving constraint problems on a matrix with small value ranges.
Recommended for interviews: Hash set validation is the expected solution for most interviews because it is easy to reason about and clearly expresses the constraint checks. Bit manipulation demonstrates deeper optimization and stronger low-level problem solving. Showing the hash-set approach first proves correctness, then mentioning the bitmask optimization signals strong algorithmic maturity.
This approach uses hash sets to track the unique numbers found in each row, column, and 3x3 sub-box. We iterate over each cell in the board, and check if the current value has been seen before in the current row, column, or sub-box. If it has, we return false. Otherwise, we add the value to the respective sets.
This solution uses three dictionaries, each containing sets, to keep track of seen values in rows, columns, and boxes. The key for the boxes dictionary is a tuple representing the box index. As we iterate, we skip any empty cells and check if the number already exists in any of its corresponding set. If it does, the Sudoku is invalid. Otherwise, we add the number to the corresponding sets and continue.
C
Java
C++
C#
JavaScript
Time Complexity: O(1) because the board size is constant (9x9).
Space Complexity: O(1) due to the fixed size of additional data structures used.
This approach seeks to replace sets with bit manipulations to compress space usage for tracking seen numbers. We increment bits in an integer representing whether a digit has been seen in rows, columns, or boxes.
This solution uses integers to record seen numbers in binary form. Each place represents whether a number has been seen (responding to the power of 2 in terms of bit location).
C
Java
C++
C#
JavaScript
Time Complexity: O(1) as the board size and iteration steps remain constant.
Space Complexity: O(1) due to the integer used for tracking positions, which is constant.
| Approach | Complexity |
|---|---|
| Approach 1: Hash Set Validation | Time Complexity: O(1) because the board size is constant (9x9). |
| Approach 2: Bit Manipulation | Time Complexity: O(1) as the board size and iteration steps remain constant. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Hash Set Validation | O(81) | O(81) | Best for clarity and interviews; easy to implement with row, column, and box tracking |
| Bit Manipulation | O(81) | O(1) | When optimizing memory or demonstrating bitmask techniques |
Valid Sudoku - Amazon Interview Question - Leetcode 36 - Python • NeetCode • 391,872 views views
Watch 9 more video solutions →Practice Valid Sudoku with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor