Skip to content
Onsite

Valid Sudoku

MediumP2Coding

Given a partially filled 9 x 9 Sudoku board, return true if the filled cells are valid so far, and false otherwise. Empty cells are ".".

The board is valid when:

  • Each row has no repeated digit among 1-9.
  • Each column has no repeated digit among 1-9.
  • Each of the nine 3 x 3 boxes (the grid split at rows and columns 3 and 6) has no repeated digit among 1-9.

Only the filled cells are checked. The board does not have to be solvable.

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
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

Example 3

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

Constraints

  • board.length == 9 and board[i].length == 9
  • Each cell is a digit from '1' to '9' or '.'.

Hints

Run checks the 3 visible tests, Submit all 12.