5. The longest subroutine string
var longestPalindrome = function(s) {
if (s.length < 2) return s If the length is less than 2, return s
let l = 0, r = 0
for (let i = 0; i < s.length; i++) {
palindrome(i, i)
palindrome(i, i+1)}function palindrome(n, m) {
while (n >= 0 && m < s.length && s[n] === s[m]) { // Left edge right edge
n--
m++
}
if (m - n > r - l) {
r = m
l = n
}
}
return s.slice(l+1, r)
}
Copy the code