#=================================== # Tanzila Islam # Email: tanzilamohita@gmail.com # Language: Python 3 #=================================== class Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: # 1. Each row must contain the digits 1-9 without repetition. # row for row in range(9): dig_arr = [0] * 10 for col in range(9): if board[row][col].isdigit(): num = int(board[row][col]) if dig_arr[num] == 0: dig_arr[num] = 1 else: return 0 # 2. Each column must contain the digits 1-9 without repetition. # column for col in range(9): dig_arr = [0] * 10 for row in range(9): if board[row][col].isdigit(): num = int(board[row][col]) if dig_arr[num] == 0: dig_arr[num] = 1 else: return 0 # 3. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition. # sub-boxes for row in range(0, 9, 3): for col in range(0, 9, 3): dig_arr = [0] * 10 for i in range(row, row+3): for j in range(col, col+3): if board[i][j].isdigit(): num = int(board[i][j]) if dig_arr[num] == 0: dig_arr[num] = 1 else: return 0 return 1
Sunday, 16 October 2022
Array - 36. Valid Sudoku
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment