Palindrome
1 min readJan 15, 2020
5. Longest Palindromic Substring
Runtime: 964 ms, faster than 71.67% of Python3 online submissions for Longest Palindromic Substring.Memory Usage: 12.7 MB, less than 100.00% of Python3 online submissions for Longest Palindromic Substring.class Solution:
def longestPalindrome(self, s: str) -> str:
if not s:return ""
def helper(l,r):
while l>=0 and r<len(s) and s[l]==s[r]:
l -= 1
r += 1
return s[l+1:r]
even_max = max((helper(i,i+1) for i in range(len(s))), key=len)
odd_max = max((helper(i,i) for i in range(len(s))), key=len)
return max(even_max, odd_max, key=len)