call(back)
Algorithms & Data StructureseasySign in to star problems

Valid Sudoku

Given a 9×9 board, decide whether the filled cells are valid: no repeated digit in any row, any column, or any 3×3 sub-box. Empty cells are '.' and need no validation. The board does not have to be solvable — only the filled cells are checked.

The board arrives as nine strings of nine characters.

["53..7....",
 "6..195...",
 ".98....6.",
 "8...6...3",
 "4..8.3..1",
 "7...2...6",
 ".6....28.",
 "...419..5",
 "....8..79"]   ->  true

Hints

Hint 1

Keep nine sets each for rows, columns and boxes and make a single pass over the cells; the first digit already present in any of its three sets ends the check.

Hint 2

The box index is (row ÷ 3) × 3 + column ÷ 3 with integer division — derive it out loud, because getting it wrong fails silently.

Asked at