Valid Sudoku

35 min · isValidSudoku()

An unfinished Sudoku grid can already contradict itself. You don't need to solve it to spot that contradiction.

Given a 9-by-9 board, return true when its filled cells obey all three Sudoku rules; otherwise return false:

  • No row contains the same digit twice.
  • No column contains the same digit twice.
  • No 3-by-3 sub-box — one of the nine smaller regions — contains the same digit twice.

Only filled cells matter. A '.' marks an empty cell, and a board may be valid even if it has no solution or isn't complete.

The judge supplies board as an array of 9 arrays. Each inner array contains 9 single-character strings.

Constraints

  • board.length == 9
  • board[i].length == 9
  • Every cell is one of '1' through '9', or '.'.

Hints

Write three honest checks

Start by checking all rows, then all columns, then each 3-by-3 sub-box. Ignore '.' and reject a unit as soon as a digit appears twice.

Track all three rules together

During one row-by-row scan, a filled cell belongs to exactly one row, one column, and one sub-box. Keep a separate seen set for each of those units.

Name the box

For a cell at (row, col), its sub-box is determined by (row // 3, col // 3). In zero-based code, Math.floor(row / 3) * 3 + Math.floor(col / 3) gives one box index from 0 through 8.

Follow-up

How would you generalize the same validation idea to an -by- board whose sub-boxes are n by n?

Visible cases

Examples

Example 1

ready
Input
board = [
  [
    "5",
    ".",
    ".",
    "6",
    ".",
    ".",
    "9",
    ".",
    "."
  ],
  [
    ".",
    "7",
    ".",
    ".",
    "9",
    ".",
    ".",
    "4",
    "."
  ],
  [
    ".",
    ".",
    "8",
    ".",
    "4",
    ".",
    ".",
    "6",
    "."
  ],
  [
    "8",
    ".",
    ".",
    ".",
    "6",
    ".",
    ".",
    ".",
    "3"
  ],
  [
    ".",
    "2",
    ".",
    "8",
    ".",
    "3",
    ".",
    "9",
    "."
  ],
  [
    "7",
    ".",
    ".",
    ".",
    "2",
    ".",
    ".",
    ".",
    "6"
  ],
  [
    ".",
    "6",
    ".",
    ".",
    "3",
    ".",
    ".",
    "8",
    "."
  ],
  [
    ".",
    ".",
    "7",
    ".",
    "1",
    ".",
    "6",
    ".",
    "."
  ],
  [
    "3",
    ".",
    ".",
    "2",
    ".",
    ".",
    "1",
    ".",
    "9"
  ]
]
Expected
true
Why
every filled digit is unique in its row, column, and box

Example 2

ready
Input
board = [
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    "7",
    ".",
    ".",
    ".",
    ".",
    ".",
    "7",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ]
]
Expected
false
Why
the third row contains 7 twice

Example 3

ready
Input
board = [
  [
    "4",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    "4",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ],
  [
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "."
  ]
]
Expected
false
Why
the top-left 3-by-3 box contains 4 twice

Interview signal

Asked at

AmazonAppleUber
Loading editor

Console

ready to run

Ready to run.

3 cases are queued.

Run: visible + custom · Submit: full suite