Description
Find the longest palindromic substring.
Constraints
- Time Complexity:
O(n²) - Space Complexity:
O(1)
Tags
stringdynamic-programming
def longestPalindrome(s):
res = ""
resLen = 0
for i in range(len(s)):
l, r = i, i
while l >= 0 and r < len(s) and s[l] == s[r]:
if (r - l + 1) > resLen:
res = s[l:r+1]
resLen = r - l + 1
l -= 1
r += 1
l, r = i, i + 1
while l >= 0 and r < len(s) and s[l] == s[r]:
if (r - l + 1) > resLen:
res = s[l:r+1]
resLen = r - l + 1
l -= 1
r += 1
return res