Introduction
Palindromic Substrings is one of the most important String Dynamic Programming problems.
You are given:
string sGoal:
Count all palindromic substrings present in the string. A palindrome:
Reads the same forward and backward. Example
s = "aaa" Output:
6 Explanation:
"a""a"
"a"
"aa"
"aa"
"aaa"Total:
6 Key Observation
Every palindrome satisfies:
First character = Last character And:
Inner substring must also be palindrome. DP State:
Inner substring must also be palindrome. Transition:
s[i] == s[j] and (j - i < 2 or dp[i+1][j-1]) Approach 1 : Memoization
Explanation
For every substring:
- Check matching ends.
- Check inner substring.
- Store result.
- Reuse calculations.
Approach 2 : Tabulation
Explanation
State:
dp[i][j] = Palindrome statusIf:
s[i] == s[j]and:
Length <= 2 or dp[i+1][j-1]Then:
dp[i][j] = true Increase answer count.
Dry Run
Input:
s = "aaa"DP Table:
a
a
a
aa
aaaaa
Count:
6 Answer:
6Approach 3 : Expand Around Center
Explanation
Every palindrome has a center.
Possible centers:
Character Center or Gap Center Expand outward while:
left == right Count every valid palindrome.
Practice
Complexity Analysis
Dynamic ProgrammingTime Complexity: O(n²)Space Complexity: O(n²)
Expand Around Center Time Complexity: O(n²)
Space Complexity: O(1)
Why This Problem is Important
- String DP
- Interval DP
- Palindrome Problems
- State Design
- Foundation for LPS
Common Beginner Mistakes
- Confusing substring with subsequence
- Missing even-length palindromes
- Incorrect DP traversal
- Wrong center expansion logic
- Forgetting single characters
Interview Tip
Always explain:
Every palindrome has a center.Then mention:
Expand Around Center uses O(1) space and is preferred.Related Questions
- Longest Palindromic Subsequence
- Longest Palindromic Substring
- Count Palindromic Subsequences
- Word Break
- Partition Labels
Final Takeaway
Palindromic Substrings is one of the most important String DP problems.
It teaches:
- String Dynamic Programming
- Palindrome Detection
- Interval DP
- Center Expansion
Mastering this problem makes advanced palindrome and string DP questions much easier to solve.