# Word Break

> Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

> For example, given
s = "leetcode",
dict = ["leet", "code"].

> Return true because "leetcode" can be segmented as "leet code".

`dp(i) = dp(j) && s[j, i) in dict, 0 <= j < i`

```c++
class Solution {
public:
bool wordBreak(string s, unordered_set &dict) {
int len = (int)s.size();
vector dp(len + 1, false);
dp[0] = true;

for(int i = 1; i <= len; i++) {
for(int j = i - 1; j >= 0; j--) {
if(dp[j] && dict.find(s.substr(j, i - j)) != dict.end()) {
dp[i] = true;
break;
}
}
}
return dp[len];
}
};
```

# World Break II

> Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

> Return all such possible sentences.

> For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

> A solution is ["cats and dog", "cat sand dog"].

1. dp[i][j] = true && dp[i][j] in dict，这种情况是这个子串直接在字典中
2. dp[i][j] = true && dp[i][j] not in dict，这种情况是这个子串不在字典中，但是它能切分成更小的子串，而这些子串在字典中
3. dp[i][j] = false，子串不能被切分

```c++

class Solution {
public:
vector >dp;
vector vals;
string val;

vector wordBreak(string s, unordered_set &dict) {
int len = (int)s.size();
dp.resize(len);
for(int i = 0; i < len; i++) {
dp[i].resize(len + 1, 0);
}

for(int i = 1; i <= len; i++) {
for(int j = 0; j < len -i + 1; j++) {
//直接存在于字典中，是第1种情况
if(dict.find(s.substr(j, i)) != dict.end()) {
dp[j][i] = 1;
continue;
}
//如果不存在，则看子串是不是能被切分，这是第2中情况
for(int k = 1; k < i && k < len -j; k++) {
if(dp[j][k] && dp[j + k][i - k]) {
dp[j][i] = 2;
break;
}
}
}
}

//不能切分，不用dfs了
if(dp[0][len] == 0) {
return vals;
}

dfs(s, 0);
return vals;
}

void dfs(const string& s, int start) {
int len = (int)s.size();
if(start == len) {
vals.push_back(val);
return;
}

for(int i = 1; i <= len - start;i++) {
if(dp[start][i] == 1) {
int oldLen = (int)val.size();
if(oldLen != 0) {
val.append(" ");
}
val.append(s.substr(start, i));

//我们从start + i开始继续dfs
dfs(s, start + i);
val.erase(oldLen, string::npos);
}
}
}
};
```