Skip to content

5. 最长回文子串 #56

@JesseZhao1990

Description

@JesseZhao1990

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/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions