9 lines C++ solution


  • 2
    E
        bool wordBreak(string s, unordered_set<string>& wordDict) {
        vector<int> res(s.length()+1,0);
        int SLength=s.length();
        res[0]=1;
        for(int i=0;i<SLength;i++)
            if(res[i]==1)
                for(int j=1;j<=SLength-i;j++)
                    if(wordDict.find(s.substr(i,j))!=wordDict.end())
                        res[i+j]=1;
        return res[SLength]==1?true:false;
        }

  • 1
    R

    using vector<bool> is better, because you just need to keep two states


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.