My python with explanation


  • 0
    S
        def validWordSquare(self, words):
            """
            :type words: List[str]
            :rtype: bool
            """
            if words: 
                n = len(words) # number of rows
                columns = [ "" for i in range(n) ] # as many number of rows as columns in a square
                for word in words:
                    for i,c in enumerate(word): 
                        if i < n: # don't care for chars in strings beyond number of rows or columns
                            columns[i] += c
                
                for row,col in zip(words,columns):
                    if row != col: # if row and column are differnt 
                        return False
            return True

Log in to reply
 

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