Given a string s
, find the longest palindromic substring in s
. A palindromic substring is a substring that reads the same forwards and backward.
Write a function longestPalindrome(s)
that takes a string s
as input and returns the longest palindromic substring of s
. If multiple palindromic substrings of the same maximum length exist, you may return any one of them.
Here are some examples to illustrate the problem:
Example 1:
s = "babad"
"bab"
or "aba"
Example 2:
s = "cbbd"
"bb"
Example 3:
s = "a"
"a"
Example 4:
s = "ac"
"a"
Example 5:
s = "racecar"
"racecar"
Let's tackle the problem of finding the longest palindromic substring within a given string.
The most straightforward approach is to examine all possible substrings and check if each one is a palindrome. We can iterate through all possible starting positions and lengths of substrings.
Code (Python):
python
def longestPalindrome_bruteforce(s):
longest = ""
for i in range(len(s)):
for j in range(i, len(s)):
substring = s[i
A more efficient solution uses dynamic programming. We can build a table dp
where dp[i][j]
is True
if the substring s[i:j+1]
is a palindrome, and False
otherwise.
dp[i][i] = True
(single characters are palindromes)dp[i][i+1] = True
if s[i] == s[i+1]
(two adjacent characters are palindromes if they are equal)dp[i][j] = True
if s[i] == s[j]
and dp[i+1][j-1] == True
Code (Python):
python def longestPalindrome_dp(s): n = len(s) dp = [[False] * n for _ in range(n)] start = 0 max_len = 1
for i in range(n):
dp[i][i] = True
for i in range(n - 1):
if s[i] == s[i + 1]:
dp[i][i + 1] = True
start = i
max_len = 2
for k in range(3, n + 1):
for i in range(n - k + 1):
j = i + k - 1
if s[i] == s[j] and dp[i + 1][j - 1]:
dp[i][j] = True
if k > max_len:
start = i
max_len = k
return s[start:start + max_len]
dp
table, which has size n x n, in O(1) time per cell.Another optimal solution involves expanding around the center. The idea is that a palindrome can be centered around a single character or between two characters. For each possible center, we expand outwards as long as the characters on both sides are equal.
s[left] == s[right]
.Code (Python):
python def longestPalindrome_expand(s): def expandAroundCenter(s, left, right): while left >= 0 and right < len(s) and s[left] == s[right]: left -= 1 right += 1 return left + 1, right - 1
start = 0
end = 0
for i in range(len(s)):
# Odd length palindrome
left1, right1 = expandAroundCenter(s, i, i)
# Even length palindrome
left2, right2 = expandAroundCenter(s, i, i + 1)
if (right1 - left1 + 1) > (end - start + 1):
start = left1
end = right1
if (right2 - left2 + 1) > (end - start + 1):
start = left2
end = right2
return s[start:end + 1]
While the brute force solution is simple to understand, it's inefficient. The dynamic programming and expanding around the center approaches provide optimal O(n^2) time complexity for solving this problem. The expanding around center solution is generally preferred because it has O(1) space complexity compared to the DP solution's O(n^2) space complexity.