TLE! functions work fine individually on compiler. But code not working!


  • 0
    Q
    string reverse(string s)
    {
        if(s=="")
            return NULL;
        int l=0;
        int r=s.length()-1;
        char temp;
        
        while(l<r)
        {
            temp = s[l];
            s[l] = s[r];
            s[r] = temp;
            l++;
            r--;
        }
        return s;
    }
    bool isPal(string s)
    {
        int l=0;
        int r=s.length()-1;
        
        while(l<r)
        {
            if(s[l] != s[r])
                return false;
            l++; r--;
        }
        return true;
    }
    string shortestPalindrome(string s) 
    {
        int len = s.length(), i;
        
        if (len < 2)
            return s;
        
        if(isPal(s))
            return s;
        
        for(i=len-1; i>1; i--)
        {
            if(isPal(s.substr(0,i)))
                break;
        }
        s=reverse(s.substr(i,len-i)) + s;
        return s;
    }

Log in to reply
 

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