Valid Sudoku - Problem

Ever wondered how those classic number puzzle games validate their boards? Welcome to the world of Sudoku validation!

You're given a 9 ร— 9 Sudoku board that may be partially filled. Your mission is to determine whether the current state follows all Sudoku rules - but here's the catch: you only need to validate the filled cells, not solve the entire puzzle.

The Three Sacred Rules of Sudoku:

  • ๐Ÿ”ด Each row must contain digits 1-9 without repetition
  • ๐Ÿ”ต Each column must contain digits 1-9 without repetition
  • ๐ŸŸข Each 3ร—3 sub-box must contain digits 1-9 without repetition

Empty cells are represented by '.' and should be ignored during validation. A valid board doesn't need to be solvable - it just needs to follow the rules for existing numbers.

Input: A 9ร—9 matrix representing the Sudoku board
Output: true if valid, false if any rule is violated

Input & Output

example_1.py โ€” Valid Sudoku Board
$ 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
๐Ÿ’ก Note: This is a valid partially filled Sudoku board. All filled numbers follow the three rules: no duplicates in rows, columns, or 3ร—3 sub-boxes.
example_2.py โ€” Invalid Sudoku Board
$ 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
๐Ÿ’ก Note: Invalid because there are two '8's in the first column: board[0][0] = '8' and board[3][0] = '8', violating the column uniqueness rule.
example_3.py โ€” Empty Board Edge Case
$ Input: board = [[".",...,"."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]]
โ€บ Output: true
๐Ÿ’ก Note: An empty Sudoku board is considered valid since there are no digits to violate any constraints. Only filled cells need validation.

Visualization

Tap to expand
Sudoku Validation: Wedding Seating RulesEach family can appear only once per row, column, and table sectionABCAโœ—Constraint TrackingRow 0: {A, B}Row 1: {C}Column 0: {A, C}Section 0: {A, C}โŒ Duplicate A in Row 1!Valid Arrangementโœ“All families seatedaccording to rulesโŒ Invalid: Family A appears twice in same section
Understanding the Visualization
1
Setup Guest Lists
Create guest lists for each row, column, and table section to track which families are already seated
2
Check Each Guest
For each seated guest, verify their family isn't already represented in their row, column, or table section
3
Update Records
If valid, add the family to all three relevant lists; if duplicate found, the arrangement is invalid
4
Validation Complete
Once all guests are checked, the seating arrangement follows all rules
Key Takeaway
๐ŸŽฏ Key Insight: Use hash sets to track 'family presence' (digits) in each constraint (row/column/box) simultaneously, enabling single-pass validation with immediate duplicate detection.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Single pass through fixed 9ร—9 board with constant time hash set operations. In general case, O(nยฒ) where n is board dimension.

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Fixed space for 27 hash sets (9 rows + 9 columns + 9 boxes), each storing at most 9 digits

n
2n
โœ“ Linear Space

Constraints

  • board.length == 9
  • board[i].length == 9
  • board[i][j] is a digit 1-9 or '.'
  • Only filled cells need to be validated
Asked in
Microsoft 45 Amazon 38 Google 32 Apple 25
89.5K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen