416. Board Rules Check

A puzzle club is vetting half-finished number boards before publishing them. The board board is always a 9 x 9 grid of characters, and every cell is either a digit from '1' to '9' or the character '.' meaning the cell is still empty.

Return true if no rule is broken by the digits already placed, and false otherwise. The rules: a digit may appear at most once in each row, at most once in each column, and at most once in each of the nine 3 x 3 blocks that tile the board (blocks begin at rows and columns 0, 3 and 6). Empty cells never break a rule. You only judge what is on the board; you do not have to decide whether the board can be finished.

Example 1

Input:
board = [[".",".","4",".","6",".",".",".","."],["7",".",".",".","2",".",".","1","."],[".","9",".",".",".",".",".","3","."],["3",".",".","9",".",".",".",".","4"],[".",".","8",".",".",".","5",".","."],["1",".",".",".",".","5",".",".","2"],[".","4",".",".",".",".",".","8","."],[".",".",".","1",".",".","9",".","."],[".",".",".",".","3",".","2",".","."]]
Output:
true
Explanation:

No digit repeats within any row, column or 3 x 3 block, so the board is acceptable.

Example 2

Input:
board = [[".",".","4",".","6",".",".",".","4"],["7",".",".",".","2",".",".","1","."],[".","9",".",".",".",".",".","3","."],["3",".",".","9",".",".",".",".","4"],[".",".","8",".",".",".","5",".","."],["1",".",".",".",".","5",".",".","2"],[".","4",".",".",".",".",".","8","."],[".",".",".","1",".",".","9",".","."],[".",".",".",".","3",".","2",".","."]]
Output:
false
Explanation:

The digit 4 now appears twice in the first row, which breaks the row rule.

Example 3

Input:
board = [[".",".","4",".","6",".",".",".","."],["7",".",".",".","2",".",".","1","."],["4","9",".",".",".",".",".","3","."],["3",".",".","9",".",".",".",".","4"],[".",".","8",".",".",".","5",".","."],["1",".",".",".",".","5",".",".","2"],[".","4",".",".",".",".",".","8","."],[".",".",".","1",".",".","9",".","."],[".",".",".",".","3",".","2",".","."]]
Output:
false
Explanation:

The two 4s sit in different rows and columns but in the same top-left 3 x 3 block, which breaks the block rule.

Constraints

board.length == 9 and board[i].length == 9

board[i][j] is a digit from '1' to '9' or '.'

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 240 msC++ 60 msJava 120 msJavaScript 120 msTypeScript 120 ms

Expected complexity

Time
O(1)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…