#36
Valid Sudoku
medium· Arrays & Hashingruns: 0Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated. Each row, each column, and each of the nine 3x3 sub-boxes must contain the digits 1 to 9 without repetition. Empty cells are marked with '.'.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 639
from collections import defaultdict
class Solution:
def isValidSudoku(self, board: list[list[str]]) -> bool:
rows = defaultdict(set)
cols = defaultdict(set)
boxes = defaultdict(set)
for r in range(9):
for c in range(9):
cell = board[r][c]
if cell == ".":
continue
box = (r // 3, c // 3)
if cell in rows[r] or cell in cols[c] or cell in boxes[box]:
return False
rows[r].add(cell)
cols[c].add(cell)
boxes[box].add(cell)
return True
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.