Imagine you're creating a word crossword puzzle where words read the same both horizontally and vertically! Given an array of unique strings words, you need to find all possible word squares you can build.
A word square is a sequence of strings where the kth row and kth column read exactly the same string. Think of it as a perfect symmetric grid where grid[i][j] == grid[j][i] for all positions.
Example: The words ["ball", "area", "lead", "lady"] form a valid word square:
b a l l a r e a l e a d l a d y
Notice how column 0 reads "ball" (same as row 0), column 1 reads "area" (same as row 1), and so on!
Goal: Return all possible word squares you can construct. The same word can be used multiple times, and order doesn't matter.
Input & Output
Constraints
- 1 โค words.length โค 1000
- 1 โค words[i].length โค 4
- All words[i] have the same length
- words[i] consists of only lowercase English letters
- All the strings of words are unique