Ctrl K

Sudoku Board Without Clashes

Medium
You are given a partly filled 9×9 sudoku board grid. Each cell holds a digit from '1' to '9' or a dot '.' for an empty cell. Check whether the digits already written clash with each other: - no digit appears twice in any row; - no digit appears twice in any column; - no digit appears twice in any of the nine 3×3 boxes the board is divided into. Return True if all three rules hold and False otherwise. You do not have to solve the puzzle: a board without clashes may still have no solution, and that does not matter. Only the filled cells are checked.
Example 1
grid=[['.', '.', '4', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '4', '.', '.', '.', '.', '.', '.']] → False

Two 4s share column 2, which breaks the rules.

Example 2
grid=[['1', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '1']] → True

The two 1s are in different rows, columns and 3×3 boxes, so nothing clashes.

Example 3
grid=[['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '7', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '7', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.']] → False

The 7s at (1, 1) and (2, 2) are in different rows and columns but inside the same top-left 3×3 box.

Constraints
  • len(grid) == 9
  • len(grid[i]) == 9
  • grid[i][j] is a digit from '1' to '9' or '.'
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsZero Out the Crosses MediumMultiply Mostly-Zero Matrices MediumLonely Dark Pixels MediumDark Pixels in Twin Rows MediumMagic 3×3 Blocks Medium
loading Python…
Input
grid=[['.', '.', '4', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '4', '.', '.', '.', '.', '.', '.']]
Expected
False

Run checks the first 3 tests in your browser. Submit checks all 22 tests on the server, hidden ones included.