AC C++, use old set again but not see any fast...need to simplify


  • 0
    class Solution {
    public:
        bool wordBreak(string s, vector<string>& wordDict) 
        {
            unordered_set<string> set;
            for(auto word : wordDict) set.insert(word);
            vector<bool> checker(s.size()+1, false);
            checker[0]=true;
            for(int i=1; i<=s.size(); i++) 
            {
                for(int j=i-1; j>=0; j--)
                {
                    if(checker[j] && set.find(s.substr(j,i-j))!=set.end())
                    {
                        checker[i] = true;
                        break;
                    }
                }
            }
            return checker[s.size()];
        }
    };
    

Log in to reply
 

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