Valid Sudoku - Problem

Determine if a 9 × 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
• Each of the nine 3 × 3 sub-boxes must contain the digits 1-9 without repetition

Note: A Sudoku board (partially filled) could be valid but is not necessarily solvable. Only the filled cells need to be validated according to the mentioned rules.

Input & Output

Example 1 — Valid Sudoku
$ 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: Each row, column, and 3×3 sub-box contains unique digits 1-9 (ignoring empty cells). No duplicates found in any region.
Example 2 — Invalid Sudoku (Row Duplicate)
$ 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: The first column contains two '8's (at positions [0,0] and [3,0]), which violates the column uniqueness rule.
Example 3 — Invalid Sudoku (Sub-box Duplicate)
$ Input: board = [["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","5",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"]]
Output: false
💡 Note: The top-left 3×3 sub-box contains two '5's (at positions [0,0] and [2,2]), which violates the sub-box uniqueness rule.

Constraints

  • board.length == 9
  • board[i].length == 9
  • board[i][j] is a digit 1-9 or '.'.

Visualization

Tap to expand
Valid Sudoku: Check Rows, Columns, and 3×3 Sub-boxesInput: 9×9 Sudoku Board53.6...98Sample top-left 3×3 sub-boxValidation Process✓ Check each row for duplicates✓ Check each column for duplicates✓ Check each 3×3 box for duplicatesUse Hash SetsTrack seen digits for each regionO(1) lookup for duplicatesOutputtrueValidorfalseInvalidBox index formula: (row÷3)×3 + (col÷3)
Understanding the Visualization
1
Input Board
9×9 grid with digits 1-9 and empty cells '.'
2
Check Rules
Validate rows, columns, and 3×3 sub-boxes for duplicates
3
Result
Return true if valid, false if duplicates found
Key Takeaway
🎯 Key Insight: Use hash sets to detect duplicates efficiently - each digit can appear at most once in each row, column, and 3×3 sub-box
Asked in
Amazon 65 Apple 45 Google 38 Microsoft 32
185.0K Views
High Frequency
~15 min Avg. Time
8.5K 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