Given a string S. The task is to count the non-overlapping pairs of palindromic sub-strings S1 and S2 such that the strings should be S1[L1…R1] and S2[L2…R2] where 0 ≤ L1 ≤ R1 < L2 ≤ R2 < N. The task is to count the number of pairs of the non-overlapping palindromic sub-strings.
Input: s = “aaa”
All possible pairs are (s, s), (s, s),
(s, s[1, 2]), (s, s) and (s[0, 1], s)
Input: s = “abacaba”
Approach: We can use Dynamic Programming to solve the above problem. We can initially create the DP table which stores if substring[i….j] is palindrome or not. We maintain a boolean dp[n][n] that is filled in a bottom-up manner. The value of dp[i][j] is true if the substring is a palindrome, otherwise false. To calculate dp[i][j], we first check the value of dp[i+1][j-1], if the value is true and s[i] is same as s[j], then we make dp[i][j] true. Otherwise, the value of dp[i][j] is made false. The following steps can be followed thereafter to get the number of pairs.
- Create a left array, where left[i] stores the count of the number of palindromes to the left on the index i including i.
- Create a right array, where right[i] stores the count of the number of palindromes to the right on the index i including i.
- Iterate from 0 to length-1 and add left[i]*right[i+1]. The summation of it for every index will be the required number of pairs.
Below is the implementation of the above approach:
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.
- Permutation of given string that maximizes count of Palindromic substrings
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Lexicographically all Shortest Palindromic Substrings from a given string
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Count of Palindromic substrings in an Index range
- Count all Prime Length Palindromic Substrings
- Count of K-size substrings having palindromic permutations
- Make palindromic string non-palindromic by rearranging its letters
- Generate a String of having N*N distinct non-palindromic Substrings
- Rearrange the string to maximize the number of palindromic substrings
- Check if a string can be split into even length palindromic substrings
- Count of pairs of strings whose concatenation forms a palindromic string
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Count ways to split a Binary String into three substrings having equal count of zeros
- Find the count of palindromic sub-string of a string in its sorted form
- Count of unique palindromic strings of length X from given string
- Count All Palindromic Subsequence in a given String
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.