Skip to main content

5 - Longest Palindromic Substring

Details

KeyValue
Linkhttps://leetcode.com/problems/longest-palindromic-substring/
LanguagePython 3
Runtime935 ms, faster than 80.41% of Python3 online submissions for Longest Palindromic Substring
Memory Usage13.9 MB, less than 90.69% of Python3 online submissions for Longest Palindromic Substring
DatastructuresList[str]
AlgorithmsDP (bottom-up)
ComplexityTime: O(n^2) Memory: O(1)

Procedure

  1. ...

Code

class Solution:
def longestPalindrome(self, s: str) -> str:
def getLongestPalindrome(s):
longest_palindrome = ""
for i in range(len(s)):
odd = palindromeAt(s, i, i)
even = palindromeAt(s, i, i+1)
longest_palindrome = max(longest_palindrome, odd, even, key=len)
return longest_palindrome

def palindromeAt( s, left, right):
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return s[left+1:right]

return getLongestPalindrome(s)