Validate Partially Filled Sudoku Grid
Arrays & HashingAuto-graded · 20 test casesLast updated
Tests ability to check constraint satisfaction across multiple overlapping groups using efficient duplicate detection.
You receive a 9 x 9 Sudoku grid where some cells contain digits and others are blank. Your task is to verify whether the current state obeys Sudoku's placement rules.
Write a function that returns True if the grid satisfies all of the following conditions, and False otherwise:
- Each row contains no repeated digits among its filled cells
- Each column contains no repeated digits among its filled cells
- Each of the nine
3 x 3subgrids contains no repeated digits among its filled cells
The nine subgrids are formed by dividing the board into a 3×3 arrangement: rows 0-2, 3-5, 6-8 crossed with columns 0-2, 3-5, 6-8.
Important: You are only checking the current state for conflicts. Blank cells never violate any rule. You do not need to determine whether the puzzle has a valid completion.
Constraints
- The input is always a
9 x 9grid:len(board) == 9andlen(board[i]) == 9 - Each cell contains either a digit character
"1"through"9", or"."representing a blank cell - The grid may be completely empty, partially filled, or completely filled
Examples
Example 1
Input: validate_sudoku([['2', '.', '.', '5', '.', '.', '1', '.', '.'], ['.', '3', '.', '.', '9', '.', '.', '6', '.'], ['.', '.', '7', '.', '.', '4', '.', '.', '8'], ['9', '.', '.', '6', '.', '.', '3', '.', '.'], ['.', '8', '.', '.', '7', '.', '.', '4', '.'], ['.', '.', '5', '.', '.', '1', '.', '.', '2'], ['4', '.', '.', '8', '.', '.', '7', '.', '.'], ['.', '1', '.', '.', '2', '.', '.', '5', '.'], ['.', '.', '6', '.', '.', '3', '.', '.', '9']])
Output: True
Example 2
Input: validate_sudoku([['2', '.', '.', '5', '.', '.', '1', '.', '.'], ['7', '3', '.', '.', '9', '.', '.', '6', '.'], ['.', '.', '7', '.', '.', '4', '.', '.', '8'], ['9', '.', '.', '6', '.', '.', '3', '.', '.'], ['.', '8', '.', '.', '7', '.', '.', '4', '.'], ['.', '.', '5', '.', '.', '1', '.', '.', '2'], ['4', '.', '.', '8', '.', '.', '7', '.', '.'], ['.', '1', '.', '.', '2', '.', '.', '5', '.'], ['.', '.', '6', '.', '.', '3', '.', '.', '9']])
Output: False
Hints
Hint 1
Track which digits appear in each row, column, and subgrid independently to detect duplicates efficiently
Hint 2
Use integer division to map any cell position to its subgrid: both the row and column indices can be divided by 3
Hint 3
A single set with composite keys can replace multiple separate data structures if you tag each entry appropriately
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that validation reduces to detecting duplicates within 27 distinct groups: 9 rows, 9 columns, and 9 subgrids. Rather than maintaining 27 separate data structures, we can use a single hash set containing composite keys that encode both the group type and the digit.
For each filled cell at position (r, c) containing digit v, we construct three unique identifiers:
('r', r, v)— encodes "digit v appears in row r"('c', c, v)— encodes "digit v appears in column c"('b', r // 3, c // 3, v)— encodes "digit v appears in subgrid at block position (r//3, c//3)"
The character prefix ('r', 'c', 'b') prevents false collisions between different group types. For example, digit 5 in row 2 and digit 5 in column 2 produce different keys.
We traverse the grid in row-major order. For each filled cell, we attempt to add its three keys to the set. If any key already exists, we've found a duplicate within that group, so we immediately return False. If we successfully process all cells without collision, the grid is valid.
Subgrid indexing: Integer division maps coordinates to subgrid positions. Rows 0-2 map to block row 0, rows 3-5 to block row 1, and rows 6-8 to block row 2. The same applies to columns. The pair (r // 3, c // 3) uniquely identifies one of the nine subgrids.
Why this works: Every duplicate must appear twice in some group. When we encounter the second occurrence, the first has already registered its key, triggering the collision. Blank cells are explicitly skipped and never contribute keys.
Complexity:
- Time: O(1) for a fixed 9×9 board, since we visit 81 cells and perform constant-work per cell. For an n×n generalization with √n×√n subgrids, this becomes O(n²).
- Space: O(1) for the fixed board, as the set holds at most 81×3 = 243 keys. Generally O(n²).
def validate_sudoku(board: list[list[str]]) -> bool:
seen = set()
for r in range(9):
for c in range(9):
v = board[r][c]
if v == '.':
continue
keys = (('r', r, v), ('c', c, v), ('b', r // 3, c // 3, v))
for key in keys:
if key in seen:
return False
seen.add(key)
return True