Given a string s, return the longest palindromic substring in s.
Expand Around Center
result to store the longest palindromic substring found.length to store the length of the longest palindromic substring found.l and r to the current character.result and length if a longer palindromic substring is found.l to the current character and r to the next character.result and length if a longer palindromic substring is found.result.class Solution:
def longestPalindrome(self, s: str) -> str:
result = ""
length = 0
for i in range(len(s)):
# odd length
l, r = i, i
while l >= 0 and r < len(s) and s[l] == s[r]:
if (r - l + 1) > length:
result = s[l:r+1]
length = r - l + 1
l -= 1
r += 1
# even length
l, r = i, i + 1
while l >= 0 and r < len(s) and s[l] == s[r]:
if (r - l + 1) > length:
result = s[l:r+1]
length = r - l + 1
l -= 1
r += 1
return result