Word Squares
Given a list of words that all have the same length L, return every word square that can be formed.
A word square is an L by L grid where row i read from left to right equals column i read from top to bottom, for every i. Equivalently, the list of rows w0, w1, ..., wL-1 satisfies w[i][j] == w[j][i] for every pair of positions.
Every row must be one of the supplied words. A word may be used in several rows, and the same word square must not be returned twice. If no word square exists, return an empty list.
Examples
Input: ["area","lead","wall","lady","ball"]
Output: [["ball","area","lead","lady"],["wall","area","lead","lady"]]
Input: ["abat","baba","atan","atal"]
Output: [["baba","abat","baba","atal"],["baba","abat","baba","atan"]]
Hints
Build a map from every prefix to the words that start with it.
Place rows one at a time. For the next row, the required prefix is the column formed by the letters already placed.
Only try words that start with that prefix.
Related Problems
Word Squares
Given a list of words that all have the same length `L`, return every word square that can be formed.