valid_sudoku

 1# @leet start
 2class Solution:
 3    def isValidSudoku(self, board: list[list[str]]) -> bool:
 4        """
 5        This function calculates if a sudoku is valid.
 6        To do so, only the digits 1-9 are allowed for all rows, cols, and 3x3 boxes.
 7        The 3x3 box location can be calculated by (r // 3) * 3 + c // 3.
 8        """
 9        N = 9
10        rows = [set() for _ in range(N)]
11        cols = [set() for _ in range(N)]
12        boxes = [set() for _ in range(N)]
13
14        for r in range(N):
15            for c in range(N):
16                val = board[r][c]
17                if val == ".":
18                    continue
19
20                if val in rows[r]:
21                    return False
22                rows[r].add(val)
23
24                if val in cols[c]:
25                    return False
26                cols[c].add(val)
27
28                idx = (r // 3) * 3 + c // 3
29                if val in boxes[idx]:
30                    return False
31                boxes[idx].add(val)
32
33        return True
34
35
36# @leet end
37def test():
38    assert 2 + 2 == 4
class Solution:
 3class Solution:
 4    def isValidSudoku(self, board: list[list[str]]) -> bool:
 5        """
 6        This function calculates if a sudoku is valid.
 7        To do so, only the digits 1-9 are allowed for all rows, cols, and 3x3 boxes.
 8        The 3x3 box location can be calculated by (r // 3) * 3 + c // 3.
 9        """
10        N = 9
11        rows = [set() for _ in range(N)]
12        cols = [set() for _ in range(N)]
13        boxes = [set() for _ in range(N)]
14
15        for r in range(N):
16            for c in range(N):
17                val = board[r][c]
18                if val == ".":
19                    continue
20
21                if val in rows[r]:
22                    return False
23                rows[r].add(val)
24
25                if val in cols[c]:
26                    return False
27                cols[c].add(val)
28
29                idx = (r // 3) * 3 + c // 3
30                if val in boxes[idx]:
31                    return False
32                boxes[idx].add(val)
33
34        return True
def isValidSudoku(self, board: list[list[str]]) -> bool:
 4    def isValidSudoku(self, board: list[list[str]]) -> bool:
 5        """
 6        This function calculates if a sudoku is valid.
 7        To do so, only the digits 1-9 are allowed for all rows, cols, and 3x3 boxes.
 8        The 3x3 box location can be calculated by (r // 3) * 3 + c // 3.
 9        """
10        N = 9
11        rows = [set() for _ in range(N)]
12        cols = [set() for _ in range(N)]
13        boxes = [set() for _ in range(N)]
14
15        for r in range(N):
16            for c in range(N):
17                val = board[r][c]
18                if val == ".":
19                    continue
20
21                if val in rows[r]:
22                    return False
23                rows[r].add(val)
24
25                if val in cols[c]:
26                    return False
27                cols[c].add(val)
28
29                idx = (r // 3) * 3 + c // 3
30                if val in boxes[idx]:
31                    return False
32                boxes[idx].add(val)
33
34        return True

This function calculates if a sudoku is valid. To do so, only the digits 1-9 are allowed for all rows, cols, and 3x3 boxes. The 3x3 box location can be calculated by (r // 3) * 3 + c // 3.

def test():
38def test():
39    assert 2 + 2 == 4