LC 0036 [M] Valid Sudoku - ALawliet/algorithms GitHub Wiki

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        seen = set()
        for r in range(9):
            for c in range(9):
                x = board[r][c]
                if x != '.':
                    check_row = f'{x} already found in row {r}' # (r,x)
                    check_col = f'{x} already found in col {c}' # (x,c)
                    check_box = f'{x} already found in box {r//3} {c//3}' # (r//3,c//3,x)
                    for check in [check_row, check_col, check_box]:
                        if check in seen: return False
                        seen.add(check)
        return True
class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        seen = set()
        return not any(x in seen or seen.add(x)
                   for r, row in enumerate(board)
                   for c, x in enumerate(row)
                   if x != '.'
                   for x in [(r,x), (x,c), (r//3,c//3,x)]
                   )
                        
class Solution:
    def isValidSudoku(self, board):
        seen = []
        for r, row in enumerate(board):
            for c, x in enumerate(row):
                if x != '.':
                    seen += [(r,x),(x,c),(r//3,c//3,x)]
        return len(seen) == len(set(seen))