# Determine if a Sudoku is valid,# according to: Sudoku Puzzles - The Rules.## The Sudoku board could be partially filled,# where empty cells are filled with the character '.'.## A partially filled sudoku which is valid.## Note:# A valid Sudoku board (partially filled) is not necessarily solvable.# Only the filled cells need to be validated.
AC
classSolution():defisValidSudoku(self, board):for i in range(9):
ifnot self.isValidList([board[i][j] for j in range(9)]) ornot self.isValidList([board[j][i] for j in range(9)]):
returnFalsefor m in range(3):
for n in range(3):
ifnot self.isValidList([board[i][j] for i in range(m*3, m*3+3) for j in range(n*3, n*3+3)]):
returnFalsereturnTruedefisValidList(self, xs):
tmp = list(filter(lambda x : x != ".", xs))
return len(tmp) + 1 == len(set(xs))
if __name__ == "__main__":
board = [[1, '.', '.', '.', '.', '.', '.', '.', '.'],
['.', 2, '.', '.', '.', '.', '.', '.', '.'],
['.', '.', 3, '.', '.', '.', '.', '.', '.'],
['.', '.', '.', 4, '.', '.', '.', '.', '.'],
['.', '.', '.', '.', 5, '.', '.', '.', '.'],
['.', '.', '.', '.', '.', 6, '.', '.', '.'],
['.', '.', '.', '.', '.', '.', 7, '.', '.'],
['.', '.', '.', '.', '.', '.', '.', 8, '.'],
['.', '.', '.', '.', '.', '.', '.', '.', 9]]
assert Solution().isValidSudoku(board)
QDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.Note:A valid Sudoku board (par