Sudoku Validator - Problem

A Sudoku puzzle is a 9×9 grid filled with digits 1-9. A valid Sudoku has the following properties:

  • Each row contains the digits 1-9 without repetition
  • Each column contains the digits 1-9 without repetition
  • Each of the nine 3×3 sub-boxes contains the digits 1-9 without repetition

Given a completed 9×9 Sudoku board, determine if it is valid.

Note: The input will always be a complete 9×9 grid with digits 1-9 only (no empty cells).

Input & Output

Example 1 — Valid Sudoku
$ Input: board = [[5,3,4,6,7,8,9,1,2],[6,7,2,1,9,5,3,4,8],[1,9,8,3,4,2,5,6,7],[8,5,9,7,6,1,4,2,3],[4,2,6,8,5,3,7,9,1],[7,1,3,9,2,4,8,5,6],[9,6,1,5,3,7,2,8,4],[2,8,7,4,1,9,6,3,5],[3,4,5,2,8,6,1,7,9]]
Output: true
💡 Note: This is a valid completed Sudoku. Each row, column, and 3×3 box contains all digits 1-9 exactly once.
Example 2 — Invalid Row
$ Input: board = [[5,3,5,6,7,8,9,1,2],[6,7,2,1,9,5,3,4,8],[1,9,8,3,4,2,5,6,7],[8,5,9,7,6,1,4,2,3],[4,2,6,8,5,3,7,9,1],[7,1,3,9,2,4,8,5,6],[9,6,1,5,3,7,2,8,4],[2,8,7,4,1,9,6,3,5],[3,4,5,2,8,6,1,7,9]]
Output: false
💡 Note: The first row has two 5s at positions 0 and 2, making it invalid.
Example 3 — Invalid Box
$ Input: board = [[5,3,4,6,7,8,9,1,2],[6,7,2,1,9,5,3,4,8],[1,9,5,3,4,2,5,6,7],[8,5,9,7,6,1,4,2,3],[4,2,6,8,5,3,7,9,1],[7,1,3,9,2,4,8,5,6],[9,6,1,5,3,7,2,8,4],[2,8,7,4,1,9,6,3,5],[3,4,5,2,8,6,1,7,9]]
Output: false
💡 Note: The top-left 3×3 box contains two 5s, violating the Sudoku rules.

Constraints

  • board.length == 9
  • board[i].length == 9
  • 1 ≤ board[i][j] ≤ 9

Visualization

Tap to expand
Sudoku Validator Problem OverviewINPUT: 9×9 Sudoku Board534678912672195348198342567...Complete 9×9 gridwith digits 1-9Top-left 3×3 boxALGORITHM: Validation Steps1Check RowsEach row has 1-9 once2Check ColumnsEach column has 1-9 once3Check 3×3 BoxesEach box has 1-9 once4Use Hash SetsTrack seen numbersOptimizationSingle pass with setsO(1) duplicate detectionEarly terminationRESULTtrueValid SudokuAll constraints satisfied:✓ Rows: no duplicates✓ Columns: no duplicates✓ Boxes: no duplicatesComplexityTime: O(1) - fixed 9×9Space: O(1) - sets sizebounded by 27×9Key Insight:Use hash sets to track numbers as you scan the board - this allows instant duplicate detection in O(1) time per cell, making validation efficient in a single pass.TutorialsPoint - Sudoku Validator | Hash Set Approach
Asked in
Apple 15 Microsoft 12 Amazon 8
32.0K Views
Medium Frequency
~12 min Avg. Time
890 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