Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Dynamic Programming High frequency Solution Available Practice Mode

Longest Palindromic Substring LeetCode Solution

Tags: #String #Dynamic Programming

Approach Summary

Expand around each center. Track the longest expansion found. Returns the actual substring, not just the length.

Practice Mode 2 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Dynamic Programming Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Dynamic Programming pattern.

Framework: Dynamic Programming • Interview Prep: Tested at FAANG • Explore all Dynamic Programming problems & invariants →

How to Recognize This Pattern

  • "Find the longest palindrome"
  • Expand-around-center beats O(n²) DP space-wise

Complexity Analysis

Time Complexity

O(n²)

Space Complexity

O(1)

Tags

String Dynamic Programming

Frequently Asked Questions

What is the optimal time and space complexity for Longest Palindromic Substring?
The optimal solution for Longest Palindromic Substring achieves O(n²) runtime complexity and requires O(1) auxiliary space. Time complexity is O(n^2) because there are 2n - 1 possible centers, and expanding outward from each takes at most O(n) steps. Space complexity is O(1) auxiliary space since expansions only update coordinate pointers.
How do I identify that Longest Palindromic Substring requires the Dynamic Programming pattern?
In technical interviews, look for key problem signals: "Find the longest palindrome"; Expand-around-center beats O(n²) DP space-wise. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Dynamic Programming strategy.
What critical edge cases must be handled in Longest Palindromic Substring?
Essential edge cases to verify include: Single character string (e.g. "a"; returns "a"); String with no multi-character palindrome (e.g. "abc"; returns any single character); Entire string is a palindrome (e.g. "racecar"; returns full string); Even length palindrome (e.g. "cbbd"; returns "bb"). Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Longest Palindromic Substring?
Longest Palindromic Substring is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Microsoft, Google, Facebook.
Buy me a coffee
Daily Practice
0/0