Skip to main content

Valid Sudoku

MediumPremium

Determine whether a partly filled 9 x 9 Sudoku board is valid so far. Only the filled cells need to follow the rules:

  • Each row contains the digits 1 to 9 at most once.
  • Each column contains the digits 1 to 9 at most once.
  • Each of the nine 3 x 3 boxes contains the digits 1 to 9 at most once.

Empty cells are written as ".". A valid board doesn't have to be solvable: you're only checking the digits already on it.

Example 1

Input: board = . . . . . . 7 . . . . 6 . 8 . . 2 3 7 . . 1 . 3 . 5 6 . 3 4 . . 7 . . . 5 . . . . 1 . 3 4 8 9 . 2 . 4 5 . . 3 . . 6 . 8 . . 2 . . . 9 1 2 . 4 . . . 2 . . . 6 . . Output: true

No digit repeats in any row, column or box.

Example 2

Input: board = . . . . 9 . . . . . . . . . . . . . . . . . . . . . . . 7 . . 2 . 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Output: false

The fourth row has two 7s.

Constraints

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