> 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/140.-word-break-ii.md).

# 140. Word Break II

```java
class Solution {
    public List<String> wordBreak(String s, List<String> wordDict) {
        List<String> result = new ArrayList<>();
        dfs(s, wordDict, 0, new ArrayList<>(), result);
        return result;
    }
    private void dfs(String s, List<String> wordDict, int start, List<String> list, List<String> res) {
        if (start == s.length()) {
            res.add(String.join(" ", list));
            return;
        }
        for (String w : wordDict) {
            int len = w.length(); // 我覺得很難想的是這個, 用 w 的 len 去抓 string 來比較, 但這樣的確很高效...
            if (start + len > s.length()) {
                continue;
            }
            String cur = s.substring(start, start + len);
            if (!w.equals(cur)) {
                continue;
            }
            
            // 如果比對後是存在 wordDict 沒錯的話(滿足 prefix), 就加入 list
            list.add(w);
            dfs(s, wordDict, start + len, list, res); // next substring part
            list.remove(list.size()-1); // backtracking
        }
    }
}
```

### backtracking 2

use each index for substring

````java
```java
class Solution {
    public List<String> wordBreak(String s, List<String> wordDict) {
        List<String> res = new ArrayList<>();
        dfs(0, s, new HashSet<>(wordDict), new ArrayList<>(), res);
        return res;
    }
    private void dfs(int start, String s, Set<String> set, List<String> list, List<String> res) {
        if (start == s.length()) {
            res.add(String.join(" ", list));
            return;
        }
        for (int i = start; i < s.length(); i++) {
            String str = s.substring(start, i+1);
            if (set.contains(str)) {
                list.add(str);
                dfs(i+1, s, set, list, res);
                list.remove(list.size()-1);
            }
        }
    }
}
```
````


---

# 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:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/140.-word-break-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
