1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public: bool wordBreak(string s, vector<string>& wordDict) { unordered_set<string> wordSet(wordDict.begin(), wordDict.end()); vector<bool> dp(s.size()+1, false); dp[0] = true; for(int i = 1; i<=s.size(); i++){ for(int j = 0; j<i; j++){ string subS = s.substr(j, i-j); if(wordSet.find(subS) != wordSet.end() && dp[j]){ dp[i] = true; } } } return dp[s.size()]; } };
|