Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Matrices
Sets
Valid Sudoku

Check whether a partially filled 9×99\times 9 Sudoku board satisfies all Sudoku rules.

Acceptance 100%
Problem Statement

You are given a partially filled 9×99 \times 9 Sudoku board. Some cells contain digits from 1 to 9, and empty cells are marked with '.'.

Determine whether the current board configuration is valid.

A valid Sudoku board must satisfy all of the following rules:

  1. Each row contains no duplicate digits from 1 to 9.
  2. Each column contains no duplicate digits from 1 to 9.
  3. Each of the nine 3×33 \times 3 sub-boxes contains no duplicate digits from 1 to 9.

Only the filled cells need to be checked; the board does not need to be solvable.

Input Format

  • A 9×99 \times 9 grid of characters.
  • Each cell is either a digit from 1 to 9 or '.' for empty.

Output Format

  • Return true if the board is valid according to Sudoku rules.
  • Otherwise, return false.

Constraints

  • The board size is fixed at 9×99 \times 9.
  • Empty cells marked '.' should be ignored.
  • Digits are limited to 1 through 9.
Examples
Sample cases returned by the problem API.

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

Explanation

No row, column, or 3x3 sub-box contains duplicate filled digits.

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

Explanation

The digit 8 appears twice in the first column, so the board is invalid.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.