Valid Sudoku
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.
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.
- the board is always exactly 9 rows by 9 columns
- filled cells hold digits 1 through 9; empty cells are a placeholder character
- validity only checks current placements, never solvability
- the answer is a single true/false verdict
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.
A 4×4 mini board with 2×2 boxes. Scan cell by cell — row, column, and box sets track what's already placed.
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 Trueclass Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
for r in range(9):
seen = set()
for c in range(9):
v = board[r][c]
if v != ".":
if v in seen:
return False
seen.add(v)
for c in range(9):
seen = set()
for r in range(9):
v = board[r][c]
if v != ".":
if v in seen:
return False
seen.add(v)
for br in range(0, 9, 3):
for bc in range(0, 9, 3):
seen = set()
for r in range(br, br + 3):
for c in range(bc, bc + 3):
v = board[r][c]
if v != ".":
if v in seen:
return False
seen.add(v)
return Trueclass Solution {
public boolean isValidSudoku(char[][] board) {
Set<String> seen = new HashSet<>();
for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++) {
char v = board[r][c];
if (v == '.') {
continue;
}
int b = (r / 3) * 3 + c / 3;
if (!seen.add("r" + r + v) || !seen.add("c" + c + v) || !seen.add("b" + b + v)) {
return false;
}
}
}
return true;
}
}class Solution {
public boolean isValidSudoku(char[][] board) {
for (int r = 0; r < 9; r++) {
Set<Character> seen = new HashSet<>();
for (int c = 0; c < 9; c++) {
char v = board[r][c];
if (v != '.' && !seen.add(v)) {
return false;
}
}
}
for (int c = 0; c < 9; c++) {
Set<Character> seen = new HashSet<>();
for (int r = 0; r < 9; r++) {
char v = board[r][c];
if (v != '.' && !seen.add(v)) {
return false;
}
}
}
for (int br = 0; br < 9; br += 3) {
for (int bc = 0; bc < 9; bc += 3) {
Set<Character> seen = new HashSet<>();
for (int r = br; r < br + 3; r++) {
for (int c = bc; c < bc + 3; c++) {
char v = board[r][c];
if (v != '.' && !seen.add(v)) {
return false;
}
}
}
}
}
return true;
}
}✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED