Skip to content
Decoder

Valid Sudoku

Medium

Problem

A partly filled 9 by 9 Sudoku board is given as a list of 9 strings, one per row. Each character is a digit 1 to 9 or . for an empty cell. Return true if the board breaks no rule so far:

- no digit repeats in any row, - no digit repeats in any column, - no digit repeats **in any of the nine 3 by 3 boxes**.

Only the filled cells need to be checked. You do not have to decide whether the puzzle can be solved.

Example 1

Input
row 0 = "1.......1"
Output
false
Why
two 1s in row 0

Example 2

row 0 = "1........", row 1 = ".1......."

Example 3

                            →  false  (two 1s in the top-left box)

Example 4

Input
an empty board
Output
true

Constraints

  • board.length == 9
  • board[i].length == 9
  • board[i][j] is a digit 1-9 or '.'.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.