Skip to content
BASA DSA

Valid Sudoku

MediumArrays & Hashing

Check the filled cells of a 9 by 9 Sudoku board. Each row, column and aligned 3 by 3 box must have no repeated digit. Dots are empty; you need not determine whether the board is solvable.

Example 1

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

Explanation

4 and 7 have different rows and columns and are different digits within their box.

Example 2

Input
board = [["4",".",".",".",".",".",".",".","4"],[".","7",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]]
Output
false

Explanation

Row 0 repeats 4 at columns 0 and 8.

Example 3

Input
board = [["4",".",".",".",".",".",".",".","."],[".","7",".",".",".",".",".",".","."],[".",".","4",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]]
Output
false

Explanation

The top-left box repeats 4 at (0,0) and (2,2), despite different rows and columns.

Constraints

  • Board is exactly 9 by 9
  • Cells are dots or digits 1..9

Function contractisValidSudokuArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…