MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / isValidSudoku

Method isValidSudoku

python/0036-valid-sudoku.py:2–21  ·  view source on GitHub ↗
(self, board: List[List[str]])

Source from the content-addressed store, hash-verified

1class Solution:
2 def isValidSudoku(self, board: List[List[str]]) -> bool:
3 cols = collections.defaultdict(set)
4 rows = collections.defaultdict(set)
5 squares = collections.defaultdict(set) # key = (r /3, c /3)
6
7 for r in range(9):
8 for c in range(9):
9 if board[r][c] == ".":
10 continue
11 if (
12 board[r][c] in rows[r]
13 or board[r][c] in cols[c]
14 or board[r][c] in squares[(r // 3, c // 3)]
15 ):
16 return False
17 cols[c].add(board[r][c])
18 rows[r].add(board[r][c])
19 squares[(r // 3, c // 3)].add(board[r][c])
20
21 return True

Callers

nothing calls this directly

Calls 1

addMethod · 0.45

Tested by

no test coverage detected