from typing import List from collections import defaultdict class Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: rows = defaultdict(set) cols = defaultdict(set) sqrs = defaultdict(set) for row_count in range(0, len(board)): for col_count in range(0, len(board[row_count])): elem = board[row_count][col_count] if elem == ".": continue if elem in rows[row_count] or elem in cols[col_count] or elem in sqrs[(row_count//3, col_count//3)]: return False rows[row_count].add(elem) cols[col_count].add(elem) sqrs[(row_count//3, col_count//3)].add(elem) col_count += 1 row_count += 1 return True cl = Solution() board = [ ["5", "3", ".", ".", "7", ".", ".", ".", "."], ["6", ".", ".", "1", "9", "5", ".", ".", "."], [".", "9", "8", ".", ".", ".", ".", "6", "."], ["8", ".", ".", ".", "6", ".", ".", ".", "3"], ["4", ".", ".", "8", ".", "3", ".", ".", "1"], ["7", ".", ".", ".", "2", ".", ".", ".", "6"], [".", "6", ".", ".", ".", ".", "2", "8", "."], [".", ".", ".", "4", "1", "9", ".", ".", "5"], [".", ".", ".", ".", "8", ".", ".", "7", "9"], ] exp = True print(f"Exp: {exp}\nRes: {cl.isValidSudoku(board)}") board = [ ["8", "3", ".", ".", "7", ".", ".", ".", "."], ["6", ".", ".", "1", "9", "5", ".", ".", "."], [".", "9", "8", ".", ".", ".", ".", "6", "."], ["8", ".", ".", ".", "6", ".", ".", ".", "3"], ["4", ".", ".", "8", ".", "3", ".", ".", "1"], ["7", ".", ".", ".", "2", ".", ".", ".", "6"], [".", "6", ".", ".", ".", ".", "2", "8", "."], [".", ".", ".", "4", "1", "9", ".", ".", "5"], [".", ".", ".", ".", "8", ".", ".", "7", "9"], ] exp = False print(f"Exp: {exp}\nRes: {cl.isValidSudoku(board)}")