Super pythonic solution using list comprehensions, no loop

  • 1
    class Solution(object):
        def isValidSudoku(self, board):
            :type board: List[List[str]]
            :rtype: bool
            def validateNumSet(numset):
                clean_set = [n for n in numset if n != '.']
                return len(clean_set)==len(set(clean_set))
            #Check basci validation
            if len(board) != 9 or len(board[0]) != 9:
                return False
            #Check rows
            if any(not validateNumSet(row) for row in board):
                return False
            #Check columns
            if any(not validateNumSet(col) for col in zip(*board)):
                return False
            #Check cubes
            if any(not validateNumSet(board[i][j] for i in range(r, r+3) for j in range(c, c+3)) for r in (0,3,6) for c in (0,3,6)):
                return False
            return True

Log in to reply

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