Getting runtime error .. but why!


  • 1
    R

    here is my code.. works fine in my machine..

    string longestPalindrome(string s) {

        int n = s.length();
        if(n <= 1)
            return s;
        if(n == 2) {
            if(s[0] == s[1])
                return s;
            else
                return s.substr(0,1);
        }
        bool** table;
        int i; short int start=0, maxl=1;
        table = new bool*[n];
        for(i=0;i<n;i++)
            table[i] = new bool(n);
        
        for(i=0;i<n-1;i++) {
            table[i][i] = true;
            if(s[i] == s[i+1]) {
                table[i][i+1]=true;
                start =i;
                maxl = 2;
            }
            else
                table[i][i+1] = false;
        }
        table[i][i] = true;
        for(int len=3;len<=n;len++) {
            for(i=0;i<n-len+1;i++) {
                int j= i+len-1;
                if((s[i] == s[j]) && table[i+1][j-1]) {
                    table[i][j] = true;
    				if(len > maxl) {
                        maxl = len;
                        start = i;
                    }
                }
                else {
                    table[i][j] = false;
                }
                
            }
        }		
        return s.substr(start,maxl);
       
    }

Log in to reply
 

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