I am getting runtime error, but it runs finely in my local machine.


  • 0
    N

    class Solution {
    public:
    void buildLPS(int *arr,string s){
    int len=0;
    int i=1;
    arr[0]=0;
    while(i<s.size()){
    if(s[i]==s[len]){
    len++;
    arr[i]=len;
    i++;
    }
    else if(len>0){
    len=arr[len-1];
    }
    else{
    i++;
    }
    }

    }
    string shortestPalindrome(string s) {
        int *lps=new int[s.size()];
        buildLPS(lps,s);
        int i,j;
        i=0;
        int sublen=s.size();
        j=s.size()-1;
        while(i<j){
            if(s[i]==s[j]){
                i++;
                j--;
            }
            else if(i>0){
                sublen=j+1+lps[i-1];
                i=lps[i-1];
                
                
            }
            else{
                j--;
                sublen=j+1;
            }
        }
        if(sublen==s.size())
            return s;
        string temp=s.substr(sublen);
        reverse(temp.begin(),temp.end());
        s=temp+s;
        return s;
    }
    

    };


Log in to reply
 

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