easy +10 pts

Valid Sudoku Validator

Check whether a 9x9 Sudoku board satisfies row, column, and box constraints.

Write a function `is_valid_sudoku(board)` that takes a 9x9 list of lists representing a Sudoku board. Each cell contains a digit '1'-'9' or '.' for an empty cell. The board is valid if: - Each row contains no duplicate digits (ignoring '.'). - Each column contains no duplicate digits (ignoring '.'). - Each of the nine 3x3 sub-boxes contains no duplicate digits (ignoring '.'). Return `True` if the board is valid, `False` otherwise.

Constraints

The board is always a 9x9 list of lists. Each element is a string from the set {'1','2','3','4','5','6','7','8','9','.'}. You may assume the board structure is correct (9 rows, 9 columns). The time complexity should be O(9^2) = O(81).

Example

>>> is_valid_sudoku([['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']])
True

>>> is_valid_sudoku([['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']])
False
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use sets or dictionaries to track seen digits for each row, column, and box.
For the 3x3 box index, use box_idx = (r // 3) * 3 + (c // 3).
Skip cells that contain '.'.
If any digit is already seen in its row, column, or box, return False immediately.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.