Judge gives runtime error, but I can run the code with the same testcase on the judge


  • 0
    R

    Hi, here is my code, when I submit it, it gives runtime error for "aba", then I run it with the same testcase on the judge, and it works, so what's happening here judge?

    class Solution {
    public:
        string shortestPalindrome(string s) {
            if(s.size() <= 1) return s;
            int n=s.size();
            string result;
            //start from middle
            for(int i=(n+1)/2; i>=2; --i) {
                    if(expandFromCenter(s, i-1,i-1, result))
                        return result;
                    if(expandFromCenter(s, i-2, i-1, result))
                        return result;
            }
        }
    private:
        bool expandFromCenter(string& s, int l, int r, string& result) {
            int n=s.size();
            while(l<r && l>=0 && r<n){
                if(s[l]!=s[r]) return false;
                l--;
                r++;
            }
            result=s.substr(0, r);
            string first=s.substr(r, n-r);
            reverse(first.begin(), first.end());
            result=first+result;
            return true;
        }
    };

Log in to reply
 

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