Show problems as
Classic problem #36MediumArrayHash TableMatrix

Valid Sudoku

Real-world version: Depot Rack Audit
Tracked in this browser only
Read Valid Sudoku on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A depot keeps crates on a storage rack shaped as a 9 x 9 grid of slots, handed to you as rack. Every slot holds either one crate, written as a single character from '1' to '9' naming its hazard class, or nothing at all, written as '.'.

Safety policy forbids two crates of the same hazard class from sharing:

  • a rack row,
  • a rack column,
  • or a bay. The rack is cut into three horizontal strips and three vertical strips, which carves it into nine 3 x 3 bays.

Only slots that currently hold a crate are subject to the policy; empty slots never conflict with anything. You are auditing the layout as it stands and not planning the rest of the load, so a layout passes the audit even when there is no legal way to fill the remaining empty slots.

Return true when the current layout breaks none of the three rules, and false otherwise.

Examples

Example 1

Input
rack = [["1",".",".",".",".",".",".",".","."],[".",".",".",".","5",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".","3",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","9"]]
Output
true

Four crates sit on the rack and no two of them share a row, a column or a bay, so the audit passes.

Example 2

Input
rack = [["6",".",".",".",".",".",".",".","."],[".","6",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]]
Output
false

The two class 6 crates sit in different rows and different columns, but both land in the top-left bay, which the policy forbids.

Constraints

  • rack.length == 9
  • rack[i].length == 9
  • rack[i][j] is a character '1' through '9', or '.' for an empty slot

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def audit_rack(rack: list[list[str]]) -> bool:
Java
public boolean auditRack(char[][] rack)