1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| var longestPalindrome = function(s) { if(s.length < 2) return s
let start = 0 let maxLength = 1 let length = s.length
function expandAroundCenter(left,right){ while(left >= 0 && right < s.length && s[left] === s[right]){ if(right - left + 1 > maxLength){ maxLength = right - left + 1 start = left } left-- right++ } }
for(let i = 0; i < length; i++){ expandAroundCenter(i-1,i+1) expandAroundCenter(i,i+1) }
return s.substring(start,start + maxLength) };
|