139. 单词拆分

class Solution {
public:
    bool f[310];
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> hashtable;
        for(auto i:wordDict)    hashtable.insert(i);
        int n=s.size();
        s=' '+s;
        f[0]=true;
        for(int i=1;i<=n;i++)
            for(int k=1;k<=i;k++)
            {
                if(hashtable.count(s.substr(k,i-k+1)))
                    f[i]|=f[k-1];
            }
        return f[n];
    }
};
posted @ 2023-04-18 16:11  穿过雾的阴霾  阅读(23)  评论(0)    收藏  举报