Valid Sudoku

Arrays & Hashing, problem 7 of 9

Valid Sudoku

Medium

LC #36

hash setmatrixtuple keys

Not attempted yet

You get a 9x9 Sudoku board as a list of 9 rows; each cell is a one-character string: a digit "1"-"9" or "." for empty.

Return True if the filled cells break no rule:

  • no digit repeats within a row
  • no digit repeats within a column
  • no digit repeats within any of the nine 3x3 boxes

The board doesn't have to be solvable; only check what's there.

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

Example 2

Input: the same board, but the
top-left cell is "8" instead of "5"
Output: False
The top-left box now has two 8s.

Example 3

Input: a board of all "."
Output: True

Constraints

  • len(board) == 9, len(board[i]) == 9
  • board[i][j] is "1"-"9" or "."

Python

Loading draft…

Test results

9 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 6 hidden

Run examples, then submit all tests.