Skip to content
Problem 36 · LeetCode 36(opens in a new tab)

Valid Sudoku

MediumHash tables

You get a 9 × 9 Sudoku board as a grid of characters. A cell holds a digit from 1 to 9, or . when it is empty.

Decide whether the digits already on the board break a rule. The rules: no digit may appear twice in the same row, twice in the same column, or twice in the same 3 × 3 box. Empty cells are ignored.

You are not asked to solve the puzzle. A board can follow every rule and still have no solution; it counts as valid.

Example 1

Input:  board =
        . . . | 9 1 . | 3 8 5
        9 . 6 | 3 . 5 | . . .
        . 8 . | . . . | . . .
        ------+-------+------
        7 . . | 1 . . | . . 4
        1 6 . | . 5 4 | . . .
        . 5 . | . . 9 | . 6 3
        ------+-------+------
        . 9 . | . . . | . . .
        . 3 . | . 4 7 | . 9 .
        5 . . | 2 . . | . . .
Output: true
Why:    every digit appears at most once per row, column and box
2 more examples and the constraints

Example 2

Input:  board =
        7 . . | 9 1 . | 3 8 5
        9 . 6 | 3 . 5 | . . .
        . 8 . | . . . | . . .
        ------+-------+------
        7 . . | 1 . . | . . 4
        1 6 . | . 5 4 | . . .
        . 5 . | . . 9 | . 6 3
        ------+-------+------
        . 9 . | . . . | . . .
        . 3 . | . 4 7 | . 9 .
        5 . . | 2 . . | . . .
Output: false
Why:    the 7 added at row 0, column 0 repeats a 7 in its column

Example 3

Input:  board =
        6 . . | 9 1 . | 3 8 5
        9 . 6 | 3 . 5 | . . .
        . 8 . | . . . | . . .
        ------+-------+------
        7 . . | 1 . . | . . 4
        1 6 . | . 5 4 | . . .
        . 5 . | . . 9 | . 6 3
        ------+-------+------
        . 9 . | . . . | . . .
        . 3 . | . 4 7 | . 9 .
        5 . . | 2 . . | . . .
Output: false
Why:    the 6 added at row 0, column 0 repeats a 6 in its box

Constraints

  • The board has exactly 9 rows and 9 columns
  • Each cell is a digit from 1 to 9, or .

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Check every cell against its peersO(n³)
  2. One pass with seen setsO(n²)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for one 9 × 9 boardVerdict
Check every cell against its peersO(n³)
Correct; repeats work
One pass with seen setsO(n²)
Best; each cell read once

n = the side of the board, 9 here.

Edge cases to test

  • Empty board. No digits at all breaks no rule: return true.
  • Valid but unsolvable. A board can follow every rule and still have no solution. Only check the rules; do not try to solve it.
  • Clash only in a box. Two equal digits in different rows and columns, like (0, 0) and (2, 1), still clash if they share a box.
  • Same digit, nothing shared. Equal digits in different rows, columns and boxes are fine. Do not flag every repeated digit on the board.

Common mistakes

  • Computing the box as r // 3 + c // 3, which puts different boxes on the same number. It must be r // 3 * 3 + c // 3.
  • Using one set of digits for everything, so the same digit in two different rows looks like a clash.
  • Comparing a cell with itself while scanning its row, column or box, so every board looks invalid.

How to talk through it

  1. 1Clarify. Only the filled cells need checking, right? And a valid board does not have to be solvable?
  2. 2Give the direct check. For each digit, scan its row, column and box. Say it reads each cell many times.
  3. 3Name the groups. Each cell belongs to one row, one column and one box. Show how r // 3 * 3 + c // 3 numbers the boxes.
  4. 4Code the one pass. Keep a set per kind of group, check before you add, and return on the first repeat.
  5. 5Test out loud. Try the empty board, a clash that is only in a box, and two equal digits that share nothing.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?