Programming Question
Given an array of unique strings words, return all the word squares you can build from words. A sequence of strings forms a valid word square if the kth row and column read the same string for every k.
Example 1:
Input: words = ["area","lead","wall","lady","ball"]
Output: [["ball","area","lead","lady"], ["wall","area","lead","lady"]]
Example 2:
Input: words = ["abat","baba","atan","atal"]
Output: [["baba","abat","baba","atal"], ["baba","abat","baba","atan"]]
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
words[i] are unique.
Optimized Java Solution
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WordSquares {
public List<List<String>> wordSquares(String[] words) {
List<List<String>> result = new ArrayList<>();
if (words == null || words.length == 0) return result;
int len = words[0].length();
Map<String, List<String>> prefixMap = buildPrefixMap(words, len);
for (String word : words) {
List<String> square = new ArrayList<>();
square.add(word);
backtrack(len, prefixMap, result, square);
}
return result;
}
private Map<String, List<String>> buildPrefixMap(String[] words, int len) {
Map<String, List<String>> prefixMap = new HashMap<>();
for (String word : words) {
for (int i = 0; i < len; i++) {
String prefix = word.substring(0, i);
prefixMap.computeIfAbsent(prefix, k -> new ArrayList<>()).add(word);
}
}
return prefixMap;
}
private void backtrack(int len, Map<String, List<String>> prefixMap, List<List<String>> result, List<String> square) {
if (square.size() == len) {
result.add(new ArrayList<>(square));
return;
}
int step = square.size();
String prefix = buildPrefix(square, step);
for (String candidate : prefixMap.getOrDefault(prefix, new ArrayList<>())) {
square.add(candidate);
backtrack(len, prefixMap, result, square);
square.remove(square.size() - 1);
}
}
private String buildPrefix(List<String> square, int step) {
StringBuilder prefix = new StringBuilder();
for (String s : square) {
prefix.append(s.charAt(step));
}
return prefix.toString();
}
public static void main(String[] args) {
WordSquares ws = new WordSquares();
String[] words1 = {"area","lead","wall","lady","ball"};
System.out.println(ws.wordSquares(words1));
String[] words2 = {"abat","baba","atan","atal"};
System.out.println(ws.wordSquares(words2));
}
}
Explanation
- Prefix Map: Build a map where keys are prefixes and values are lists of words starting with those prefixes. This allows for quick lookup of potential candidates for forming word squares.
- Backtracking: Use backtracking to explore building squares row by row. If a complete square is formed (i.e., its size reaches the word length), add it to the result list.
- Prefix Building: During backtracking, build the prefix necessary for the current row by gathering the kth character from each previous word in the square. This prefix helps fetch candidates from the prefix map efficiently.