</> MAANG.io
coding interview · 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Palindromic DP

What is this?

A palindrome reads the same forwards and backwards, which gives it a tidy inside-out structure: a stretch of text is a palindrome only if its two ends match and the smaller stretch inside it is one too. Palindromic DP uses that fact by solving the short pieces first and remembering them, so each longer piece just checks its ends and looks up the inner answer it already has. It is like peeling outward from the middle, never re-checking a center you have already grown from.

flowchart TD A["A span from i to j"] --> B["Do the two ends match"] B --> C["Look up the inner span already solved"] C --> D["Solve shorter spans before longer ones"] D --> E["Track the longest palindrome found"]

💡 Fun fact: A naive palindrome search can be quadratic, but Manacher's algorithm finds the longest palindromic substring in linear time by reusing mirror information across centers — a rare case where the clever trick brings an O(n^2) problem all the way down to O(n).

🔓 The 1 problem in this chapter is free. Sign in with Google or Microsoft to start solving.


Core idea: A palindrome reads the same forwards and backwards, which gives it a recursive inside-out structure: s[i..j] is a palindrome iff its ends match and the span inside it is too. That single fact powers two techniques — a DP table filled by increasing length, or an O(1)-space expand-around-center scan.


Two ways to exploit symmetry

Two ways to exploit symmetry: DP table dp[i][j] = (s[i]==s[j]) and dp[i+1][j-1] filled by length (inside first), versus expand around each of 2n-1 centers growing outward while ends match in O(1) space

The DP makes the inside-out dependency explicit (you must compute shorter spans before longer ones); expand-around-center is the same idea run from the middle of each potential palindrome.


The problem

Palindromic DP: inside-out bucket pointing to its problem: Longest Palindromic Substring

  • Longest Palindromic Substring — find the longest contiguous palindrome via the dp table (fill by length) or expand-around-center (handle both odd and even centers); Manacher's gives O(n) for the curious.

Key takeaways

  • Palindromes are inside-out — ends match and the interior is a palindrome.
  • DP fills by length because dp[i][j] depends on the shorter dp[i+1][j-1].
  • Expand-around-center is the clean O(1)-space default — remember the even-length centers.
  • Why interviewers love it: it tests recognizing recursive symmetry and handling the odd/even gotcha.

Problem: Longest Palindromic Substring.

Sign in to MAANG.io

Use your Google or Microsoft account — no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.