Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool wordBreak(string s, vector<string>& wordDict) {
- set<string> word_set(wordDict.begin(), wordDict.end());
- vector<bool> dp(s.length() + 1);
- dp[0] = true;
- for(int i = 1; i <= s.length(); i++) {
- for(int j = 0; j < i; j++) {
- if(dp[j] && word_set.find(s.substr(j, i - j)) != word_set.end()) {
- dp[i] = true;
- break;
- }
- }
- }
- return dp[s.length()];
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment