#5 Longest Palindromic Substring
frequently reported atTCSMicrosoftBloombergAmazonByteDance / TikTokWalmart
the problem
In plain words
Return the longest contiguous substring of s that reads the same forwards and backwards. If there are ties, any one is accepted.
in s = "babad"
out "bab"
"aba" is also valid.
in s = "cbbd"
out "bb"
- 1 ≤ s.length ≤ 1000
- Digits and English letters
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternExpand around centerInterval DP
s[i..j] is a palindrome when its ends match and its inside s[i+1..j−1] is a palindrome. Equivalently, every palindrome grows outward from a centre.
solutions
From brute force to optimal
For all (i, j), test palindrome with two pointers.
- Skip substrings no longer than the best so far.
- Keep the longest palindrome.
n² substrings × O(n) check.
Indices only (plus the answer).
class Solution { public String longestPalindrome(String s) { String best = ""; for (int i = 0; i < s.length(); i++) { for (int j = i; j < s.length(); j++) { if (j - i + 1 > best.length() && isPalindrome(s, i, j)) best = s.substring(i, j + 1); } } return best; } private boolean isPalindrome(String s, int lo, int hi) { while (lo < hi) if (s.charAt(lo++) != s.charAt(hi--)) return false; return true; }}pal[i][j] = ends match && pal[i+1][j−1], filled by increasing length.
- Lengths 1 and 2 only need the ends to match.
- Track the longest true cell.
n² intervals × O(1).
The boolean table (1 MB for n = 1000).
class Solution { public String longestPalindrome(String s) { int n = s.length(), start = 0, bestLen = 1; boolean[][] pal = new boolean[n][n]; // pal[i][j]: is s[i..j] a palindrome? for (int len = 1; len <= n; len++) { // short intervals first for (int i = 0; i + len - 1 < n; i++) { int j = i + len - 1; // ends match AND the inside is a palindrome (inside of length <= 1 always is) pal[i][j] = s.charAt(i) == s.charAt(j) && (len <= 2 || pal[i + 1][j - 1]); if (pal[i][j] && len > bestLen) { bestLen = len; start = i; } } } return s.substring(start, start + bestLen); }}2n − 1 centres (characters and gaps); grow each while ends match.
- For each c: expand(c, c) for odd, expand(c, c+1) for even.
- Update the best start/end.
2n − 1 centres × up to O(n) expansion.
No table.
class Solution { public String longestPalindrome(String s) { int start = 0, end = 0; // best palindrome is s[start..end] for (int c = 0; c < s.length(); c++) { int odd = expand(s, c, c); // centre on a character: "aba" int even = expand(s, c, c + 1); // centre between two: "abba" int len = Math.max(odd, even); if (len > end - start + 1) { start = c - (len - 1) / 2; end = c + len / 2; } } return s.substring(start, end + 1); } /** Grows outward while both ends match; returns the palindrome length found. */ private int expand(String s, int lo, int hi) { while (lo >= 0 && hi < s.length() && s.charAt(lo) == s.charAt(hi)) { lo--; hi++; } return hi - lo - 1; }}Grow from every centre
in the room
How to say it in the interview
talk track
- Present the DP to show the interval-DP idea, then say expand-around-centre has the same time but O(1) space.
traps
- Forgetting even-length palindromes ("abba" has a centre between the two b's).
- Filling the DP table by i ascending: pal[i+1][j−1] isn't ready yet. Fill by length.
what they ask next
Follow-ups
Linear time?
Manacher's algorithm reuses mirror information for O(n). Rarely expected in interviews.
Longest Palindromic Subsequence (LC 516)?
Interval DP: dp[i][j] = 2 + dp[i+1][j−1] on match, else max(dp[i+1][j], dp[i][j−1]). Or LCS(s, reverse(s)).
before you submit
Edge cases to test
- One character → itself.
- No repeats ("ac") → any single character.
- Even length: "cbbd" → "bb" needs the gap centre between the two b's.
practise next
Similar problems
- #125Valid PalindromeEasyin Two Pointers
- #131Palindrome PartitioningMediumin Backtracking
- #214Shortest PalindromeHardin Range Queries & Strings