Given an array of unique strings words, return all theword squaresyou can build fromwords. The same word from words can be used multiple times. You can return the answer in any order.
A sequence of strings forms a valid word square if the kth row and column read the same string, where 0 <= k < max(numRows, numColumns).
For example, the word sequence ["ball","area","lead","lady"] forms a word square because each word reads the same both horizontally and vertically.
Example 1:
**Input:** words = ["area","lead","wall","lady","ball"]
**Output:** [["ball","area","lead","lady"],["wall","area","lead","lady"]]
**Explanation:**
The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
Example 2:
**Input:** words = ["abat","baba","atan","atal"]
**Output:** [["baba","abat","baba","atal"],["baba","abat","baba","atan"]]
**Explanation:**
The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
Constraints:
1 <= words.length <= 1000
1 <= words[i].length <= 5
All words[i] have the same length.
words[i] consists of only lowercase English letters.
All words[i] are unique.
ac
classSolution {publicList<List<String>> wordSquares(String[] words) {List<List<String>> res =newArrayList<>();// edge casesif (words ==null||words.length==0) return res;// build trieTrieNode root =newTrieNode();for (String word : words) {build(root, word);root.candidates.add(word); }// backtrackingsearch(root,newArrayList<String>(), res);return res; }privatevoidsearch(TrieNode root,List<String> note,List<List<String>> res) {// exitif (note.size() >0&¬e.size() ==note.get(0).length()) {res.add(newArrayList<>(note));return; }String prefix =buildPrefix(note);List<String> candidates =getCandidates(root, prefix);for (String candi : candidates) {note.add(candi);search(root, note, res);note.remove(note.size() -1); } }privateList<String> getCandidates(TrieNode root,String prefix) {TrieNode r = root;for (int i =0; i <prefix.length(); i++) {int pos =prefix.charAt(i) -'a';if (r.next[pos] ==null) returnnewArrayList<>(); r =r.next[pos]; }returnr.candidates; }privateStringbuildPrefix(List<String> note) {StringBuilder sb =newStringBuilder();int col =note.size();for (String s : note) {if(col <s.length()) sb.append(s.charAt(col)); }returnsb.toString(); }privatevoidbuild(TrieNode root,String word) {TrieNode r = root;for (int i =0; i <word.length(); i++) {int pos =word.charAt(i) -'a';if (r.next[pos] ==null) r.next[pos] =newTrieNode(); r =r.next[pos];r.candidates.add(word); } }}classTrieNode {TrieNode[] next =newTrieNode[26];List<String> candidates =newArrayList<>();}/*trie + backtracking*/