65ms Python solution


  • 0
    E

    I am new to python
    This is a relatively neat solution for me
    Please tell me if there can be more improvements?
    '''
    class Solution(object):
    def isValidSudoku(self, board):
    """
    :type board: List[List[str]]
    :rtype: bool
    """

        def evaluation(list):
            return len(set(list)) == len(list)
    
        def column(i):
            return [row[i] for row in board]
        
        def block(i,j):
            temp = []
            for s in xrange(3):
                for t in xrange(3):
                    temp.append(board[i+s][j+t])
            return temp
            
        #row evaluation
        for row in board:
            tmp = [strRow for strRow in row if strRow != '.']
            if not evaluation(tmp):
                return False
        
        #column evaluation
        for i in xrange(9):
            tmp = [strColumn for strColumn in column(i) if strColumn != '.']
            if not evaluation(tmp):
                return False   
                
        #block evaluation
        for i in xrange(3):
            for j in xrange(3):
                tmp = [strBlock for strBlock in block(3*i,3*j) if strBlock != '.']
                if not evaluation(tmp):
                    return False
        
        return True
    

    '''


Log in to reply
 

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