KatInterview

Check a Partially Filled Number Grid for Rule Violations

Arrays & HashingAuto-graded · 14 test cases
MediumAuto-gradedArrays & Hashinghashingbitmaskmatrix

Last updated

Scan a 9x9 digit grid once and decide whether any row, column, or 3x3 block repeats a digit, using per-group bitmasks or sets.

You are given a 9x9 grid, grid, representing a partially completed number-placement puzzle. Each cell is a one-character string: either a digit from '1' to '9', or '.' for an empty cell.

Decide whether the grid, as it currently stands, obeys these rules:

  1. No digit appears more than once in any row.
  2. No digit appears more than once in any column.
  3. No digit appears more than once in any of the nine 3x3 blocks. The blocks are formed by splitting the grid into three bands of three rows and three stacks of three columns.

Return True if no rule is broken, and False otherwise.

Details

  • Only filled cells are checked. Empty cells ('.') never cause a violation, and any number of them may repeat.
  • You are judging only whether the current contents break a rule. You do not need to determine whether the puzzle can be completed. A grid that is rule-abiding but has no possible solution still returns True.
  • A grid with no filled cells at all is valid.
  • Aim for a single pass over the 81 cells, recording what each row, column and block has already seen, so a conflict is detected the moment the second copy of a digit is encountered.

Constraints

  • grid has exactly 9 rows, and each row has exactly 9 entries.
  • Every entry is one of '1', '2', '3', '4', '5', '6', '7', '8', '9', or '.'.
  • The function returns a boolean.

Examples

Example 1

Input: is_grid_consistent([['5', '3', '.', '.', '7', '.', '.', '.', '.'], ['6', '.', '.', '1', '9', '5', '.', '.', '.'], ['.', '9', '8', '.', '.', '.', '.', '6', '.'], ['8', '.', '.', '.', '6', '.', '.', '.', '3'], ['4', '.', '.', '8', '.', '3', '.', '.', '1'], ['7', '.', '.', '.', '2', '.', '.', '.', '6'], ['.', '6', '.', '.', '.', '.', '2', '8', '.'], ['.', '.', '.', '4', '1', '9', '.', '.', '5'], ['.', '.', '.', '.', '8', '.', '.', '7', '9']])

Output: True

Example 2

Input: is_grid_consistent([['8', '3', '.', '.', '7', '.', '.', '.', '.'], ['6', '.', '.', '1', '9', '5', '.', '.', '.'], ['.', '9', '8', '.', '.', '.', '.', '6', '.'], ['8', '.', '.', '.', '6', '.', '.', '.', '3'], ['4', '.', '.', '8', '.', '3', '.', '.', '1'], ['7', '.', '.', '.', '2', '.', '.', '.', '6'], ['.', '6', '.', '.', '.', '.', '2', '8', '.'], ['.', '.', '.', '4', '1', '9', '.', '.', '5'], ['.', '.', '.', '.', '8', '.', '.', '7', '9']])

Output: False

Hints

Hint 1

Every filled cell belongs to exactly three groups at once: one row, one column and one 3x3 block. Track the digits already seen in each group while scanning.

Hint 2

To map a cell at row r and column c to a block number from 0 to 8, use (r // 3) * 3 + (c // 3).

Hint 3

Represent the seen digits of each group as a 9-bit integer (bit d-1 for digit d) or as a set. Before recording a digit, test whether it is already present in its row, column or block; if so, return False right away.