5. Longest Palindromic Substring
# O(n^2) time || O(1) space
def longest_palindrome(self, s: str) -> str:
def expand(i, j):
while i >= 0 and j < len(s) and s[i] == s[j]:
i, j = i - 1, j + 1
return s[i + 1: j]
return max([expand(i, j) for i in range(len(s)) for j in (i, i + 1)], key=len)
This problem can be solved using several methods. A common approach is to expand around the center. This approach can handle even and odd length palindromes well. Here’s the idea:
For every character in the string:
- Consider the character as the center of an odd-length palindrome and expand outwards.
- Consider the character and its next character as the center of an even-length palindrome and expand outwards.
- Record the longest palindrome found.