Longest Palindromic Substring
Difficulty: Medium
Given a string s, return its longest contiguous substring that reads the same forwards and backwards. If several are tied, return any one.
Examples
Input: s = "babad"
Output: "bab"
"aba" is also a valid answer — both have length 3.
Input: s = "cbbd"
Output: "bb"
The longest palindrome is "bb"; single letters like "c" or "d" are shorter.
Input: s = "a"
Output: "a"
Constraints
1 <= s.length <= 1000
s consists of only lowercase English letters
Approach
A palindrome mirrors outward from its center. So instead of checking all ~n²/2 substrings separately, try every possible center and grow outward while the mirroring holds:
- Each character, for odd-length palindromes.
- Each gap between two characters, for even-length palindromes.
For each of the 2n - 1 centers, expansion stops as soon as the sides differ, so the scan is quadratic instead of cubic. Track the longest palindrome as you go.
Solutions
Brute Force — Check Every Substring
Check every substring by comparing characters from both ends inward, and keep the longest palindrome.
function longestPalindrome(s) {
function isPalindrome(str) {
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) return false;
left++;
right--;
}
return true;
}
let longest = "";
for (let i = 0; i < s.length; i++) {
for (let j = i; j < s.length; j++) {
const candidate = s.slice(i, j + 1);
if (candidate.length > longest.length && isPalindrome(candidate)) {
longest = candidate;
}
}
}
return longest;
}Time: O(n^3) — O(n^2) substrings, each checked in O(n) · Space: O(1) extra, ignoring the substrings themselves
Optimal — Expand Around Center
Treat every character, and every gap between two characters, as a possible center. Grow outward while both sides match, and track the widest palindrome found.
function longestPalindrome(s) {
let start = 0;
let maxLength = 1;
function expand(left, right) {
while (left >= 0 && right < s.length && s[left] === s[right]) {
left--;
right++;
}
// left/right overshot by one step; the real palindrome is (left+1 .. right-1)
const length = right - left - 1;
if (length > maxLength) {
maxLength = length;
start = left + 1;
}
}
for (let i = 0; i < s.length; i++) {
expand(i, i); // odd-length palindromes centered on i
expand(i, i + 1); // even-length palindromes centered between i and i+1
}
return s.slice(start, start + maxLength);
}Time: O(n^2) · Space: O(1) extra