Valid Word Square https://leetcode.com/problems/valid-word-square/
Two loop solution: Time O(MN) and Space O(1)
- Use two for loops. Outer loop iterates through each word row by row. The pointer is i.
- Inner for loop iterates from 0 to length of the row word. The pointer is j.
- Now word[i,j] must be equal to word[j,i].
- We are sure that word[i,j] is valid. We must also make sure we are not accessing out of bounds for word[j,i]. Therefore we have additional checks.
class Solution(object): def validWordSquare(self, words): """ :type words: List[str] :rtype: bool """ for i in range(len(words)): for j in range(len(words[i])): if j < len(words) and i < len(words[j]) and words[i][j] == words[j][i]: continue else: return False return True