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aaaaaaaaaaaaaaaaaaaa".
    I know this string is loooong, but I don't think array with 1000k long is long enough to get a memory limit exceeded..

    Does anyone have any idea where is wrong?
    Thanks!

    The following is my code:

    var longestPalindrome = function(s) {
    var i, j, left, right, ml=0, mr=0, max = 0,
    idx,
    D = Array(s.length * s.length);

    for(i=0; i<s.length; i++) {
        for(j=0; j<s.length-i; j++) {
            left = j;
            right = j+i;
            idx = left * s.length + right;
            D[idx] = s[left] === s[right] && (i<2 || D[(left+1)*s.length + right-1]);
            
            if(D[idx]) {
                if(right-left > mr - ml) {
                    ml = left;
                    mr = right;
                }
            }
        }
    }
    D = null;
    return s.substring(ml, mr+1);
    

    }


Log in to reply
 

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