> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/dfs-and-bfs/dfs/425.-word-squares.md).

# 425. Word Squares

## DFS + Backtracking + HashMap

T: O(N\*26^L), N words\[] size, L is word len

```java
class Solution {
    public List<List<String>> wordSquares(String[] words) {
        Map<String, List<String>> map = prefixMapWords(words);
        List<List<String>> res = new ArrayList<>();
        List<String> square = new ArrayList<>();
        for (String word : words) {
            square.add(word);
            search(map, res, square);
            square.remove(square.size()-1);
        }
        return res;
    }
    private Map<String, List<String>> prefixMapWords(String[] words) {
        Map<String, List<String>> map = new HashMap<>();
        for (String word : words) {
            for (int i = 0; i < word.length(); i++) {
                String prefix = word.substring(0, i+1);
                map.putIfAbsent(prefix, new ArrayList<>());
                map.get(prefix).add(word);
            }
        }
        return map;
    }
    private void search(Map<String, List<String>> map, List<List<String>> res, List<String> square) {
        int rowCount = square.get(0).length();
        int rowIndex = square.size();
        
        if (rowIndex == rowCount) {
            res.add(new ArrayList<>(square));
            return;
        }
        
        
        StringBuilder prefix = new StringBuilder();
        for (String s : square) {
            prefix.append(s.charAt(rowIndex));
        }
        
        List<String> wordWithPrefix = map.getOrDefault(prefix.toString(), new ArrayList<>());
        for (String word : wordWithPrefix) {
            square.add(word);
            search(map, res, square);
            square.remove(square.size()-1);
        }
    }
}

/*
1. generate a prefix map for each word's prefix, map<String, List<String>>

2. each word can be the square's first line,
so dfs each word (backtracking)

3. in dfs 
terminal condition: if rowCnt == rowIndex => add ans to res
make curr prefix

4. 
get this prefix's mapping words(List)
dfs these words(backtracking) agian

*/
```

## pruning  version

before processing one prefix, we can check all prefix is existed in map

```java
        for (int i = rowIndex; i < rowCount; i++) {
            StringBuilder prefix = new StringBuilder();
            for (String s : square) {
                prefix.append(s.charAt(rowIndex));
            }
            if (!map.containsKey(prefix.toString())) {
                return;
            }
        }
```

```java
class Solution {
    public List<List<String>> wordSquares(String[] words) {
        Map<String, List<String>> map = prefixMapWords(words);
        List<List<String>> res = new ArrayList<>();
        List<String> square = new ArrayList<>();
        for (String word : words) {
            square.add(word);
            search(map, res, square);
            square.remove(square.size()-1);
        }
        return res;
    }
    
    private Map<String, List<String>> prefixMapWords(String[] words) {
        Map<String, List<String>> map = new HashMap<>();
        for (String word : words) {
            for (int i = 0; i < word.length(); i++) {
                String prefix = word.substring(0, i+1);
                map.putIfAbsent(prefix, new ArrayList<>());
                map.get(prefix).add(word);
            }
        }
        return map;
    }
    private void search(Map<String, List<String>> map, List<List<String>> res, List<String> square) {
        int rowCount = square.get(0).length();
        int rowIndex = square.size();
        
        if (rowIndex == rowCount) {
            res.add(new ArrayList<>(square));
            return;
        }
        
        for (int i = rowIndex; i < rowCount; i++) {
            StringBuilder prefix = new StringBuilder();
            for (String s : square) {
                prefix.append(s.charAt(rowIndex));
            }
            if (!map.containsKey(prefix.toString())) {
                return;
            }
        }
        
        StringBuilder prefix = new StringBuilder();
        for (String s : square) {
            prefix.append(s.charAt(rowIndex));
        }
        
        List<String> wordWithPrefix = map.getOrDefault(prefix.toString(), new ArrayList<>());
        for (String word : wordWithPrefix) {
            square.add(word);
            search(map, res, square);
            square.remove(square.size()-1);
        }
    }
}

/*
1. generate a prefix map for each word's prefix, map<String, List<String>>

2. each word can be the square's first line,
so dfs each word (backtracking)

3. in dfs 
terminal condition: if rowCnt == rowIndex => add ans to res
make curr prefix

4. 
get this prefix's mapping words(List)
dfs these words(backtracking) agian

*/
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/dfs-and-bfs/dfs/425.-word-squares.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
