Output Limit Exceeded


  • 0
    L

    my algorithm is very simple. Why output limit exceeded?

    bool ispalindrome (string s, size_t start, size_t end)
    {
        while(start < end)
        {
            if(s[start] != s[end])
                return false;
            start += 1;
            end -= 1;
        }
        return true;
    }
    void get_one_result(vector< vector<string> > &allresult, vector<string> oneresult, string &s, size_t start)
    {
        if(start == s.size())
        {
            allresult.push_back(oneresult);
            return;
        }
        for(size_t end = start; end <s.size(); end += 1)
        {
            if(ispalindrome(s,start,end))
            {
                oneresult.push_back(s.substr(start,end-start+1));
                get_one_result(allresult,oneresult,s,end+1);
                oneresult.pop_back();
            }
        }
    
    }
    vector< vector<string> > partition(string s) {
        vector< vector<string> > allresult;
        vector<string> oneresult;
        get_one_result(allresult,oneresult,s,0);
        return allresult;
    }

  • 0
    N

    In case string s is very long than oneresult will be full bcoz you are popping after calling get_one_result() function so stack will be full.

    use 2D array to store palindrome... your solution will be submitted ..

    vector<vector<int>>a(n,vector<int>(n,0));

        int i ,j,k=0;
        for(i=0 ;i<n ;i++)
         a[i][i]=1;
        
        while(k!=n)
        {
            i=k; j=k+1;
            
            while(i>=0 && j<n)
            {
                if(s[i]!=s[j])
                 break; else
                  a[i][j]=1;
                i-- ; j++;
                
            }
            
            i=k-1; j=k+1;
            
            while(i>=0 && j<n)
            {
                if(s[i]!=s[j])
                 break; else a[i][j]=1;
                i-- ; j++;
                
            }
          k++;  
        }
        
        vector<vector<string>>ans;
    

  • 0
    W

    My solution is exactly same as yours. Why I get rum time error?


  • 0
    N

    Just check the boundary conditions otherwise my program is running good.
    can you post your code so that i can see errors?


Log in to reply
 

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