algorithm icon indicating copy to clipboard operation
algorithm copied to clipboard

5. 最长回文子串

Open JesseZhao1990 opened this issue 6 years ago • 0 comments

image

/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function(s) {
    if(s.length < 2) return s;

    let newS = s.split('').join("#").split('');
    newS.push("#$");
    newS.unshift("^#");

    let max = "";

    for(let k=1; k< newS.length-1; k++){
      let i = k-1;
      let j = k+1;
      while(newS[i]==newS[j]){
        i--;
        j++
      }
      let subStr = newS.slice(i+1,j).join("").replace(/#|\^|\$/g,"");
      max = max.length > subStr.length ? max : subStr;
    }

    return max;

};

https://leetcode-cn.com/problems/longest-palindromic-substring/

JesseZhao1990 avatar Oct 12 '19 07:10 JesseZhao1990