Python
No description yet. This curated deck is ready for daily study.
Card preview (1)
def is_valid_sudoku(s: str) -> bool:
from typing import List # — 1. Parse & sanity-check —————————————– # — 2. Row & column checks ——————————————- # — 3. 3×3 sub-grid checks ——————————————- from typing import List # --- 1. Parse & sanity-check ----------------------------------------- rows = [row.strip() for row in s.strip().split(';')] if len(rows) != 9: return False board: List[List[str]] = [] for row in rows: cells = [c.strip() for c in row.split(',')] if len(cells) != 9: return False if any(c not in '123456789.' for c in cells): return False board.append(cells) # --- 2. Row & column checks ------------------------------------------- for i in range(9): row_vals = [v for v in board[i] if v != '.'] if len(row_vals) != len(set(row_vals)): return False col_vals = [board[r][i] for r in range(9) if board[r][i] != '.'] if len(col_vals) != len(set(col_vals)): return False # --- 3. 3×3 sub-grid checks ------------------------------------------- for br in (0, 3, 6): for bc in (0, 3, 6): block = [ board[r][c] for r in range(br, br + 3) for c in range(bc, bc + 3) if board[r][c] != '.' ] if len(block) != len(set(block)): return False return True