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] //this condition shows that in recursion if i crosses j then it will be a invalid substring or //if i==j that means only one character is remaining and we require substring of length 2 //in both the conditions we need to return 0 Else if(i == j || i > j) return 0; 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count substrings of a given string whose anagram is a palindrome
- Count all palindrome which is square of a palindrome
- Count pair of strings whose concatenation of substrings form a palindrome
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Count All Palindrome Sub-Strings in a String | Set 2
- Count the number of vowels occurring in all the substrings of given string
- Count of substrings of a given Binary string with all characters same
- Check if all substrings of length K of a Binary String has equal count of 0s and 1s
- Count ways to split a Binary String into three substrings having equal count of zeros
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Count substrings that contain all vowels | SET 2
- Sum of all substrings of a string representing a number | Set 1
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all Prime Length Palindromic Substrings
- Count all substrings having character K
- Count of all unique substrings with non-repeating characters
- Count of substrings having all distinct characters
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)