One line python dark hack...


  • 0
    K

    Idea is to generate all row, columns and blocks, then check its validate.

    def isValidSudoku(self, board):
        return False not in map(lambda lst: len(filter(lambda y: y != '.', lst)) == len(set(filter(lambda y: y != '.', lst))), [board[x] for x in range(9)] + [''.join(zip(*board)[x]) for x in range(9)] + [''.join(board[each[0] + x][each[1] + y] for x in range(3) for y in range(3)) for each in [(3 * i, 3 * j) for i in range(3) for j in range(3)]])
    

    Equivalent to:

    def isValid(self, lst):
        lst = filter(lambda x: x != '.', lst)
        return len(lst) == len(set(lst))
    
    def isValidSudoku(self, board):
        for x in range(9):
            if not self.isValid(board[x]):
                return False
        for y in range(9):
            if not self.isValid(zip(*board)[y]):
                return False
        point = [(3 * i, 3 * j) for i in range(3) for j in range(3)]
        for each in point:
            if not self.isValid([board[each[0] + x][each[1] + y] for x in range(3) for y in range(3)]):
                return False
        return True

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.