VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-palindrome-sub-strings-string/

⇱ Palindrome Substrings Count - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Palindrome Substrings Count

Last Updated : 29 Aug, 2025

Given a string s, find the total number of palindromic substrings of length greater than or equal to 2 present in the string.
A substring is palindromic if it reads the same forwards and backwards.

Examples:

Input: s = "abaab"
Output: 3
Explanation: Palindromic substrings with length greater than 1, are "aba", "aa", and "baab".

Input: s = "aaa"
Output: 3
Explanation: Palindromic substrings with length greater than 1, are "aa" , "aa" , "aaa" .

Input: s = "abbaeae"
Output: 4
Explanation: Palindromic substrings with length greater than 1, are "bb" , "abba" , "aea", "eae".

[Naive Approach] By Generating All Possible Substrings - O(n^3) Time and O(1) Space

The idea is to generate all possible substrings using two nested loops and for every substring check if it is palindrome or not.


Output
3

[Better Approach 1] Using Memoization - O(n^2) Time and O(n^2) Space

If we notice carefully, we can observe that this recursive solution holds the following two properties of Dynamic Programming:

1.Optimal Substructure: The solution for the problem isPalindrome(i, j) depends on the optimal solution of the subproblem isPalindrome(i + 1, j - 1). By solving the smaller substructures, we can efficiently find if the entire substring is a palindrome or not.

2.Overlapping Subproblems: We can see that we are computing the same subproblems multiple times, isPalindrome(i + 2, j - 2) will be computed in isPalindrome(i, j) as well as isPalindrome(i + 1, j - 1). This redundancy leads to overlapping subproblems.

  • There are two parameters(i and j) that change in the recursive solution and then can go from 0 to n. So we create a 2D array of size n x n for memoization.
  • We initialize this array as -1 to indicate nothing is computed initially. We first check if the value is -1, then only we make recursive calls.
  • If the substring from i to j is a palindrome, we store memo[i][j] = 1, otherwise 0.

Output
3

[Better Approach 2] Using Bottom-Up DP (Tabulation) - O(n^2) Time and O(n^2) Space

We create a dp array of size n x n. However, we cannot simply fill the dp table from i = 0 to n-1 and j from i to n-1. To compute the value for (i, j), we need the value from (i+1, j-1). Similar to Matrix Chain Multiplication, we need to fill the table diagonally using a gap variable.

1. Base Cases:
=> A single character string is always palindrome, i.e. dp[i][i] = true.
=> Strings having two characters are palindrome, if both the characters are same. i.e. dp[i][i+1] = true if s[i] == s[i+1].

2. Any substring s[i...j] will be palindrome if:
=> If first and last characters of string are same
=> Remaining substring (excluding first and last character) is palindrome. I.e. dp[i+1][j-1] = true.

Illustration:



Output
3

Expected Approach - Continued article

This problem can be solved more efficiently using Manacher’s algorithm or the center expansion technique.
See: Count All Palindrome Sub-Strings Set 2

Related article:

Comment