Longest Palindromic Substring
Problem
Given a string s, return the longest substring of s that is a palindrome.
Example 1
- Input
s = "babad"- Output
"bab"- Why
- "aba" is also a valid answer.
Example 2
- Input
s = "cbbd"- Output
"bb"
Constraints
- 1 ≤ s.length ≤ 1000
- s contains digits and English letters.
Explanation
Every palindrome mirrors around a centre, which is either one character (odd length) or the gap between two characters (even length). There are only 2n - 1 centres.
From each centre, expand outwards while the characters on both sides match. That is O(n) per centre and O(1) memory, simpler than a 2-D DP table.
Approach 1 · Naive
Check every substring
- Time O(n³)
- Space O(1)
Test every substring, longest first or keeping the best, with a two-pointer palindrome check.
- For every
i ≤ j, ifs[i..j]is a palindrome and longer than the best, keep it.
class Solution {
public String longestPalindrome(String s) {
int start = 0;
int length = 1;
for (int i = 0; i < s.length(); i++) {
for (int j = i + length; j < s.length(); j++) {
if (isPalindrome(s, i, j)) {
start = i;
length = j - i + 1;
}
}
}
return s.substring(start, start + length);
}
private boolean isPalindrome(String s, int i, int j) {
while (i < j) {
if (s.charAt(i++) != s.charAt(j--)) {
return false;
}
}
return true;
}
}class Solution:
def longestPalindrome(self, s: str) -> str:
best = s[0]
for i in range(len(s)):
for j in range(i + len(best), len(s)):
piece = s[i:j + 1]
if piece == piece[::-1]:
best = piece
return bestApproach 2 · Optimal
Expand around each centre
- Time O(n²)
- Space O(1)
For each index, expand an odd-length palindrome around it and an even-length one around it and its right neighbour.
expand(left, right): while both are in range ands[left] == s[right], move outwards; return the lengthright - left - 1.- For each
i, takemax(expand(i, i), expand(i, i + 1)). - If it beats the best, record its start:
i - (len - 1) / 2.
class Solution {
public String longestPalindrome(String s) {
int start = 0;
int best = 0;
for (int i = 0; i < s.length(); i++) {
int length = Math.max(expand(s, i, i), expand(s, i, i + 1));
if (length > best) {
best = length;
start = i - (length - 1) / 2;
}
}
return s.substring(start, start + best);
}
private int expand(String s, int left, int right) {
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
left--;
right++;
}
return right - left - 1;
}
}class Solution:
def longestPalindrome(self, s: str) -> str:
def expand(left, right):
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return s[left + 1:right]
best = ""
for i in range(len(s)):
for candidate in (expand(i, i), expand(i, i + 1)):
if len(candidate) > len(best):
best = candidate
return bestComplexity compared
| Approach | Time | Space |
|---|---|---|
| Naive Check every substring | O(n³) | O(1) |
| Optimal Expand around each centre | O(n²) | O(1) |
Key takeaways
- Remember the even-length centre (
expand(i, i + 1)); "bb" has no single middle character. - Manacher's algorithm does it in O(n), but expand-around-centre is what interviews expect.
Your notes
Saved in this browser as you type.
Solved it on your own?
Mark it done to track your progress, or star it to come back before an interview.