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