Run time error longest palindromic substring .


  • 0
    R
    https://leetcode.com/submissions/detail/33609163/
    
    string pre (string s) {
    int n = s.length();
    string res ;
    if(n==0)
        return "^$";
    res = "^";
    for(int i=0;i<n;i++){
        res+="#"+s.substr(i,1);
    }
    res+="#$";
    
    return res;
    

    }

    string longestPalindrome (string s) {

    string t = pre(s);

    int n = t.length();
    int p[n];
    int r = 0,c = 0;
    int mlen = 0;
    int idx = 0;
    
    for(int i = 1;i < n - 1; i++){
        int mi_i = 2*c-1;
    
        if(r>i)
            p[i] = min(r-i,p[mi_i]);
        else
            p[i] = 0;
    
        while(t[i+1+p[i]] == t[i-1-p[i]] ){
            p[i]++;
        }
        if(i+p[i]>r){
            c = i;
            r = i+p[i];
        }
    }
    
    
    for(int i = 1;i<n-1;i++){
        if(p[i] > mlen){
            mlen = p[i];
            idx = i;
        }
    }
    return s.substr( (idx-mlen-1)/2 , mlen );
    

    }

    };


Log in to reply
 

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