139. Word Break

139. Word Break_第1张图片
image.png

参考: https://algorithm.yuanbin.me/zh-hans/dynamic_programming/word_break.html
139. Word Break_第2张图片
image.png

class Solution {
public:
    bool wordBreak(string s, vector& wordDict) {
        vector dp(s.size() + 1, false);
        dp[0] = true;
        for(int i = 1; i <= s.size(); i++){
            for(int j = 0; j < i; j++){
                if(find(wordDict.begin(), wordDict.end(),s.substr(j, i - j)) != wordDict.end() && dp[j] == true){
                    dp[i] = true;
                    continue;
                }
            }
        }
        return dp[s.size()];
    }
};

你可能感兴趣的:(139. Word Break)