Given a string, the task is to count all palindrome sub string in a given string. Length of palindrome sub string is greater than or equal to 2.
Input : str = "abaab" Output: 3 Explanation : All palindrome substring are : "aba" , "aa" , "baab" Input : str = "abbaeae" Output: 4 Explanation : All palindrome substring are : "bb" , "abba" ,"aea","eae"
We have discussed a similar problem below.
Find all distinct palindromic sub-strings of a given string
The above problem can be recursively defined.
Initial Values : i = 0, j = n-1; Given string 'str' CountPS(i, j) // If length of string is 2 then we // check both character are same or not If (j == i+1) return str[i] == str[j] Else If str[i..j] is PALINDROME // increment count by 1 and check for // rest palindromic substring (i, j-1), (i+1, j) // remove common palindrome substring (i+1, j-1) return countPS(i+1, j) + countPS(i, j-1) + 1 - countPS(i+1, j-1); Else // if NOT PALINDROME // We check for rest palindromic substrings (i, j-1) // and (i+1, j) // remove common palindrome substring (i+1 , j-1) return countPS(i+1, j) + countPS(i, j-1) - countPS(i+1 , j-1);
If we draw recursion tree of above recursive solution, we can observe overlapping Subprolems. Since the problem has overlapping sub-problems, we can solve it efficiently using Dynamic Programming. Below is a Dynamic Programming based solution.
Time complexity: O(n2)
Auxiliary Space: O(n2)
This article is contributed by Nishant_Singh(Pintu). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count pair of strings whose concatenation of substrings form a palindrome
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Count of substrings of a binary string containing K ones
- Given a binary string, count number of substrings that start and end with 1.
- Count the number of vowels occurring in all the substrings of given string
- Queries to find the count of vowels in the substrings of the given string
- Count of distinct substrings of a string using Suffix Array
- Permutation of given string that maximizes count of Palindromic substrings
- Count number of substrings of a string consisting of same characters
- Count of distinct substrings of a string using Suffix Trie
- Count All Palindrome Sub-Strings in a String | Set 2
- Count minimum swap to make string palindrome
- Count all palindrome which is square of a palindrome
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Count substrings with same first and last characters
- Count all substrings having character K
- Count substrings that contain all vowels | SET 2
- Convert the string into palindrome string by changing only one character.
- Count of substrings consisting only of vowels
- Number of substrings with count of each character as k