最长回文子串
程序员文章站
2022-07-13 21:58:38
...
最长回文子串
- 最长回文字串例如:abacc 的最长回文子串就是 aba
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function(s) {
var len = s.length
if(!len){
return s
}
var center = 0
var _center = 0
var low = 0,hei = 0
var posi = [0, 0]
while(center < len){
_center = center+1
while(s[center] === s[_center]){
_center++
}
low = center-1
hei = _center
while(low >=0 && hei <len && s[low] === s[hei]){
low--
hei++
}
low++
if(hei - low > posi[1] - posi[0]){
posi[0] = low
posi[1] = hei
}
center = _center
}
return s.slice(posi[0], posi[1])
};