Use DP, and dp[i] means if the substr s[0...i] can be broken. The broken substrings are allowed to be the same. For example, string aaa is breakable with the word dict {"a"}.
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int len=s.length();
vector<bool> dp(len,0);
for(int i=0;i<len;i++)
{
string temp=s.substr(0,i+1);
if(wordDict.find(temp)!=wordDict.end())
dp[i]=1;
else
{
for(int j=0;j<i;j++)
{
temp=s.substr(j+1,i-j);
if(dp[j]&&wordDict.find(temp)!=wordDict.end())
{
dp[i]=1;
break;
}
}
}
}
return dp[len-1];
}
};