Palindromic Substrings
O(n²)
Expand around centers, or 2D dp where dp[i][j] depends on dp[i+1][j−1] plus matching ends.
Learn firstInterval DP
0%
mastery
0/7 solved
0/7 solved
Recognise it
The signal
Longest palindromic substring/subsequence, count palindromic substrings, or min cuts to split into palindromes.
1Brute force
Check every substring O(n3).
2The observation
Rechecking substrings: inner palindromes are reused.
3Why it works
When pal[i][j] is computed, every shorter span inside it (i+1..j-1) is already correct.
Reach for it when
- +minimum cuts into palindromes
Not when
- ×n up to ~1e6, O(n²) too slowuse Manacher, O(n)
- ×Palindrome queries with updates to the stringuse Rolling hash + segment tree
Practice
0/7 solved
- Palindromic SubstringsFoundationFoundationMeta, AdobeMedium
- Longest Palindromic SubstringApply it · Longest one.Apply itAmazon, MicrosoftMedium
- Longest Palindromic SubsequenceOne change · Subsequence, not substring.One changeMedium
- Minimum Insertion Steps to Make a String PalindromeTwists · Disguised subsequence problem.TwistsHard
- Palindrome Partitioning IICombine · Partition DP on top of the pal table.CombineHard
- Longest PalindromeMastery checkMastery checkCSESMedium
- All PalindromesCSESHard
Pitfalls
Classic bugs
- !Mixing substring (contiguous, boolean table) with subsequence (can skip chars, length table): different recurrences.
- !Wrong fill order: iterate by increasing length, or i from n-1 down, so dp[i+1][j-1] is ready.
Go deepervariants and techniques · 1 combinations · resources
Variants and techniques
Combinations
Palindromic Substrings+1D / Fibonacci-style
minimum palindrome partition cuts
- Palindromic Substrings:
- O(1) palindrome check for any substring
- 1D / Fibonacci-style:
- min cuts over prefixes