Longest Palindromic Substring
Medium · Dynamic programming
Jump to codeReturn a longest contiguous substring that reads the same forward and backward. If several have the same maximum length, any is acceptable.
Examples
Example 1
s = "babad"
Output: "bab"
Example 2
s = "cbbd"
Output: "bb"
Constraints
- 1 <= s.length <= 1000
- s contains only digits and English letters.
Need a hint?
A palindrome grows symmetrically around a character or a gap.
Asked in real interviews
Candidates reported versions of Longest Palindromic Substring from these company interviews. Some company questions are locked and open with Premium or XP.
- Find the Longest Palindromic SubstringeBay
- Find the Longest Palindromic Substring in Linear TimeByteDance
- Find the longest palindromic substringTikTok
Solution approach
For each index, expand around both an odd-length center (i, i) and an even-length center (i, i+1). Stop at a mismatch or boundary. Record only the best start and length while searching, then slice once at the end. Empty input returns an empty string.
Complexity
O(n²) time and O(1) auxiliary space excluding the returned substring.
Python reference solution
def longestPalindrome(s):
start = length = 0
for center in range(len(s)):
for left, right in ((center, center), (center, center + 1)):
while left >= 0 and right < len(s) and s[left] == s[right]:
if right - left + 1 > length:
start, length = left, right - left + 1
left -= 1
right += 1
return s[start:start + length]
JavaScript solution
function longestPalindrome(s) {
let start = 0, length = 0;
for (let center = 0; center < s.length; center++) {
for (let offset = 0; offset <= 1; offset++) {
let left = center, right = center + offset;
while (left >= 0 && right < s.length && s[left] === s[right]) {
if (right - left + 1 > length) { start = left; length = right - left + 1; }
left--; right++;
}
}
}
return s.slice(start, start + length);
}
Java solution
import java.util.*;
class Solution {
public String longestPalindrome(String s) {
int start = 0, length = 0;
for (int center = 0; center < s.length(); center++) {
for (int offset = 0; offset <= 1; offset++) {
int left = center, right = center + offset;
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
if (right - left + 1 > length) { start = left; length = right - left + 1; }
left--; right++;
}
}
}
return s.substring(start, start + length);
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
int start = 0, length = 0;
for (int center = 0; center < (int)s.size(); center++) {
for (int offset = 0; offset <= 1; offset++) {
int left = center, right = center + offset;
while (left >= 0 && right < (int)s.size() && s[left] == s[right]) {
if (right - left + 1 > length) { start = left; length = right - left + 1; }
left--; right++;
}
}
}
return s.substr(start, length);
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 7 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[ "babad" ]
bab
[ "cbbd" ]
bb
[ "a" ]
a
[ "racecar" ]
racecar
[ "forgeeksskeegfor" ]
geeksskeeg
Drafts are saved per language in this browser when storage is available.