# 140. Word Break II

### Description

Given a **non-empty** string *s* and a dictionary *wordDict* containing a list of **non-empty** words, add spaces in *s* to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.

**Note:**

* The same word in the dictionary may be reused multiple times in the segmentation.
* You may assume the dictionary does not contain duplicate words.

### Constraints

### Approach

### Links

* GeeksforGeeks
* [Leetcode](https://leetcode.com/problems/word-break-ii/)
* [ProgramCreek](https://www.programcreek.com/2014/03/leetcode-word-break-ii-java/)
* YouTube

### **Examples**

{% tabs %}
{% tab title="Example 1" %}
**Input:** s = "catsanddog", wordDict = \["cat", "cats", "and", "sand", "dog"]

**Output:**

\[

"cats and dog",

"cat sand dog"

]
{% endtab %}

{% tab title="Example 2" %}
**Input:** s = "pineapplepenapple", wordDict = \["apple", "pen", "applepen", "pine", "pineapple"]

**Output:**

\[

"pine apple pen apple",

"pineapple pen apple",

"pine applepen apple"

]

**Explanation:** Note that you are allowed to reuse a dictionary word.
{% endtab %}

{% tab title="Example 3" %}
**Input:** s = "catsandog", wordDict = \["cats", "dog", "sand", "and", "cat"]

**Output:** \[]
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="Solution 1" %}

```java
/**
 * Time complexity : 
 * Space complexity : 
 */

class Solution {
    public List<String> wordBreak(String s, List<String> wordDict) {
        int sLen = s.length();
        List<String> result = new ArrayList<>();
        if(sLen == 0) return result;
        
        ArrayList<String>[] pos = new ArrayList[sLen+1];
        pos[0] = new ArrayList<String>();

        for(int i = 0; i < sLen; i++) {
            if(pos[i] != null) {
                for(int j = i+1; j <= sLen; j++) {
                    String str = s.substring(i, j);
                    if(wordDict.contains(str)) {
                        if(pos[j] == null) {
                            pos[j] = new ArrayList<String>();
                        }
                        pos[j].add(str);
                    }
                }
            }
        }
        
        if(pos[sLen] == null) return result;
        
        dfs(result, pos, "", sLen);
        
        return result;
    }
    
    private void dfs(List<String> result,
                     ArrayList<String>[] pos,
                     String currStr,
                     int i) {
        if(i == 0) {
            result.add(currStr.trim());
            return;
        }
        
        for(String word: pos[i]) {
            dfs(result, pos, word+" "+currStr, i-word.length());
        }
    }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

*


---

# Agent Instructions: 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://code-snippets.hbamithkumara.com/leetcode/problems/101-200/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.
