Valid Sudoku
Real-world version: Depot Rack AuditThe 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.
def audit_rack(rack: list[list[str]]) -> bool:public boolean auditRack(char[][] rack)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 9 test cases: 9 passed
- Passed: 2 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Find Missing and Repeated ValuesReal-world version: Kiln Tray AuditEasy
- Find Winner on a Tic Tac Toe GameReal-world version: Who Comes Out Ahead on the Three-by-ThreeEasy
- Set Matrix ZeroesReal-world version: Blank The Failed SitesMedium
- Number of Distinct IslandsReal-world version: Distinct Reed Patch ShapesMedium