DP Solution in C++ Get Output Limit Exceeded Error. Help Please


  • 0
    L

    vector<string> wordBreak(string s, unordered_set<string> &dict)
    {
    if(!s.size())
    return vector<string>();

    vector<vector<string> > vecCuts(s.size());
    vecCuts.push_back(vector<string>(1, ""));
    vector<int> cutsAfter(1, s.size());
    for(int i=s.size()-1; i>=0; i--)
    {
    	for(int k=0; k<cutsAfter.size(); k++)
    	{
    		int index = cutsAfter[k];
    		string wordCandidate = s.substr(i, index-i);
    		if(dict.find(wordCandidate)!=dict.end())
    		{
    			cutsAfter.push_back(i);
    			for(int m=0; m<vecCuts[index].size(); m++)
    				vecCuts[i].push_back(wordCandidate+" "+vecCuts[index][m]);
    		}
    	}
    }
    return vecCuts[0];
    

    }


Log in to reply
 

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