Need HELP, Memory Limit Exceed


  • 0
    A

    class Solution {
    public:

    vector<vector<string>> partition(string s) {
        
        vector<vector<string>> res;
        vector<string> tmp={s};
        res.push_back(tmp);
        if(s.empty() || s.size()==1) return res;
        partition(res);
        return res;
        
    }
    
    void partition(vector<vector<string>> &res){
        for(int i=0;i<res.back().size();i++){
            int index=findPoint(res.back().at(i));
            if(index){
                vector<string> s=res.back();
                s.insert(s.begin()+i,s[i].substr(0,index+1));
                s[i+1]=s[i+1].substr(index+1);
                res.push_back(s);
                s.clear();
                partition(res);
            }
        }
    }
    int findPoint(string &s){
        if(s.empty() || s.size()==1) return 0;
        for(int i=0;i<s.size();i++){
            if(s[i]==s[0]) {
                if(isPalin(s.substr(0,i+1)) && isPalin(s.substr(i+1)))
                    return i;
            }
        }
    }
    bool isPalin(string s){
        if(s.empty() || s.size()==1) return true;
        for(int i=0,j=s.size()-1;i<j;i++,j--){
            if(s[i]!=s[j]) return false;
        }
        return true;
    }
    

    };


  • 0
    A

    I tried to minimized the memory but still need to store the answers. How can I fix this?


Log in to reply
 

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