Skip to content
AI360Xpert
Beta
Difficulty: MediumArrays & Hashing

Valid Sudoku

Problem in Plain English

Given a partially filled 9 x 9 Sudoku board where empty cells are written as ., decide whether the filled cells obey the Sudoku rules: no digit 1-9 may repeat within any row, any column, or any of the nine 3 x 3 sub-boxes. A board can be valid without being solvable.

Problem Statement

Determine whether a partially filled 9 x 9 Sudoku board is valid. Only the filled cells must satisfy the rules: each row, each column, and each of the nine 3 x 3 sub-boxes must contain the digits 1-9 without repetition.

Empty cells are written as . and are ignored. A board that follows these rules is valid even if it could never be completed into a solved puzzle.

Constraints

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

Examples

Example 1

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

No row, column or 3 x 3 sub-box contains a repeated digit, so the filled cells satisfy every rule.

Example 2

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

Changing the top-left 5 to 8 creates a box conflict with 8 at (2, 2) in box 0. It also repeats 8 in column 0 at rows 0 and 3. Either violation is enough to return false.

Example 3

Input
board = [[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]]
Output
true

All 81 cells are empty, so there are no filled digits to violate a row, column, or box rule.

Example 4

Input
board = [["4",".",".",".",".",".",".",".","."],[".","4",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]]
Output
false

The two 4s use different rows and columns, but both belong to the top-left 3-by-3 box. Only the box rule fails.

Example 5

Input
board = [["4",".",".","4",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]]
Output
false

Two 4s share row 0 but use different columns and different boxes. Only the row rule fails.

Example 6

Input
board = [["4",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],["4",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]]
Output
false

Two 4s share column 0 but use different rows and different boxes. Only the column rule fails.

Intuition

Each filled cell belongs to three groups: its row, its column, and its 3-by-3 box. A digit is legal only when none of those groups already contains it. Example 1 puts 5 at (0, 0), so row 0, column 0, and box 0 each remember 5. A later 5 in any one of those groups would be a conflict; a 5 in unrelated groups is allowed. Empty cells carry no digit and are skipped. This checks existing clues, not whether a solver can finish the puzzle.

Approaches

Three Separate Passes

Solution Details

Reveal Three Separate Passes: intuition, complexity, and code

Hints

Hint 1
A duplicate matters only inside one row, one column or one sub-box, so each filled cell needs a fast membership test against those three groups.
Hint 2
One set per row, one per column and one per sub-box lets you test and record a digit the moment you visit its cell.
Hint 3
Integer division maps a cell to its sub-box: combine r / 3 and c / 3 into a single box index from 0 to 8.

Edge Cases

  • An all-dot board returns true. Empty rows and boxes do not need every digit to be present.
  • A box-only conflict can sit at (0, 0) and (1, 1), while every row and column is individually valid.
  • A row-only conflict at (0, 0) and (0, 3), or column-only conflict at (0, 0) and (3, 0), must also return false.
  • Equal digits in unrelated groups are allowed, such as 4 at (0, 0) and (3, 3). A single global set would wrongly reject them.
  • Validity does not imply solvability: row 0 containing .12345678 and row 1 starting with 9 has no existing duplicate, but cell (0, 0) cannot be filled legally.
  • Other characters and board shapes are outside the public constraints; this exercise does not require a generalized input validator.

Common Mistakes and Interview Tips

  • Checking only rows and columns misses box-only conflicts. Each filled cell must also satisfy its box rule.
  • Using non-integer division or (r / 3) + (c / 3) gives wrong box grouping. Use floor division and multiply the box row by 3.
  • Recording dots makes empty cells look like duplicates. Skip them before any membership check.
  • Sharing one set across groups, or constructing nine references to the same set, lets unrelated cells interfere. Create an independent set for each group.
  • Interview tip: explain why the fixed 9-by-9 constraints make both methods O(1), then compare 243 versus 81 cell inspections and one live collection versus 27. Validation needs no backtracking or puzzle solving.

Key Takeaway

When one item must satisfy several independent group constraints at once, keep one seen-set per group and validate each item against all of them in a single pass.