◀ THE GRIND — ARRAYS & HASHING

Valid Sudoku

MEDIUM✓ CHIP-TIMEDLC #36 — FULL STATEMENT ↗

The drill: Check whether a partly-filled 9×9 sudoku board breaks any rule right now — no repeated digit in a row, column, or 3×3 box. Empty cells are dots and prove nothing.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A 9×9 board arrives, partially filled with digits and otherwise marked with empty-cell placeholders, and the task is to check whether it currently breaks any sudoku placement rule.

A digit may not repeat within its row, within its column, or within its own 3×3 box — checking those three conditions for every filled cell is the entire job.

Nothing about actually solving the puzzle matters here; an incomplete board with plenty of empty cells left can still be perfectly valid, as long as none of the digits already placed collide.

EX 01
board = ["1........", ".2.......", "..3......", "...4.....", "....5....", ".....6...", "......7..", ".......8.", "........9"]
true
CLEAN DIAGONAL
EX 02
board = ["44.......", ".........", ".........", ".........", ".........", ".........", ".........", ".........", "........."]
false
ROW REPEAT
EX 03
board = ["7........", "7........", ".........", ".........", ".........", ".........", ".........", ".........", "........."]
false
COLUMN REPEAT
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

Nothing needs solving — only catching a repeat. There are exactly three kinds: row, column, box.

HINT 2 THE STRUCTURE

One set per row, per column, per box — 27 small sets. Walk each filled cell once and report the first collision.

HINT 3 ONE STEP FROM THE ANSWER

The box a cell belongs to is (r/3)·3 + c/3 with integer division. One pass, three membership checks per digit.

COACH'S BOARD — THE PATTERN, STEP BY STEP
ROW, COLUMN, BOXPATTERN · ONE PASS, THREE SETS4×4 mini board, 2×2 boxes — a box collision hiding at (1,1)
1
1
STEP 1

A 4×4 mini board with 2×2 boxes. Scan cell by cell — row, column, and box sets track what's already placed.

STEP 1 / 6 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/valid-sudoku.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        rows = [set() for _ in range(9)]
        cols = [set() for _ in range(9)]
        boxes = [set() for _ in range(9)]
        for r in range(9):
            for c in range(9):
                v = board[r][c]
                if v == ".":
                    continue
                b = (r // 3) * 3 + c // 3
                if v in rows[r] or v in cols[c] or v in boxes[b]:
                    return False
                rows[r].add(v)
                cols[c].add(v)
                boxes[b].add(v)
        return True
TIME O(N²)SPACE O(N²)PYTHON · RACE PACE · 17 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED