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 Validator INPUT 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 9x9 Sudoku Board "." = empty cell Three Rules: 1. Each row: 1-9, no repeat 2. Each col: 1-9, no repeat 3. Each 3x3 box: no repeat ALGORITHM STEPS 1 Initialize Hash Sets Create sets for rows, cols, and 3x3 boxes 2 Single Pass Scan Iterate each cell once Skip empty cells (".") 3 Check Duplicates For each digit, check if exists in row/col/box set 4 Add to Sets If no duplicate, add digit to corresponding sets Hash Set Structure rows[0] {5,3,7} cols[0] {5,6,8,4,7} box[0,0] {5,3,6,9,8} box_idx = (row/3, col/3) FINAL RESULT Row OK Col OK Box OK Output: true Board is VALID Validation Summary All 9 rows: OK All 9 columns: OK All 9 boxes: OK Key Insight: Use encoding like "row_i_num" or "col_j_num" as keys in a single HashSet, or use separate sets for each row, column, and 3x3 box. The box index is calculated as (row/3, col/3). Time: O(81) = O(1), Space: O(81) = O(1) since board size is fixed. TutorialsPoint - Valid Sudoku | Single Pass with Hash Sets Approach
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