Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created October 28, 2017 21:33
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
int len = s.size();
unordered_set<string> dict(wordDict.begin(), wordDict.end());
vector<bool> dp(len + 1, false);
dp[0] = true;
for(int i = 0; i < len; ++i)
{
for(int j = 0; j <= i; ++j)
{
string str = s.substr(j, i - j + 1);
if(dict.find(str) != dict.end() && dp[j])
{
dp[i + 1] = true;
break;
}
}
}
return dp[len];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment