Problem statement

https://leetcode.com/problems/valid-word-square/

Solution

Note, that words can have different lengths and it can still be valid word square. We asked to check if given matrix is symmetric. We can do it in $O(n^2)$ complexity, comparing rows and columns one by one. We can fill not full strings with # symbol and then compare full rows and columns, without bothering about border cases, complexity is still $O(n^2)$ but with bigger constant.

In python it can be done in oneline. The reason why it is working: we construct column by column. And if we have some gaps, it can be shown that it is working as well.

Complexity

Time complexity is $O(n^2)$, space as well, where $n$ is number of words.

Code

class Solution:
    def validWordSquare(self, words):
        return words == ["".join(word[j] for word in words if j < len(word)) for j in range(len(words[0]))]