Given a string S of length n and a positive integer k. The task is to find number of Palindromic Subsequences of length k where k <= 3.
Input : s = "aabab", k = 2 Output : 4 Input : s = "aaa", k = 3 Output : 1
For k = 1, we can easily say that number of characters in string will be the answer.
For k = 2, we can easily make pairs of same characters so we have to maintain the count of each character in string and then calculate
sum = 0 for character 'a' to 'z' cnt = count(characater) sum = sum + cnt*(cnt-1)/2 sum is the answer.
Now as k increases, it became difficult to find. How to find answer for k = 3 ? So the idea is to see that palindromes of length 3 will be of the format TZT, so we have to maintain two matrices, one to calculate the prefix sum of each character, and one to calculate suffix sum of each character in the string.
Prefix sum for a character T at index i is L[T][i] i.e number of times T has occured in the range [0, i](indices).
Suffix sum for a character T at index i is R[T] has occurred in the range [i, n – 1](indices).
Both the matrices will be 26*n and one can precompute both these matrices in complexity O(26*n) where n is the length of the string.
Now how to compute the subsequence ? Think over this: for an index i suppose a character X appears n1 times in the range [0, i – 1] and n2 times in the range [i + 1, n – 1] then the answer for this character will be n1 * n2 i.e L[X][i-1] * R[X][i + 1], this will give the count of subsequences of the format X-s[i]-X where s[i] is the character at i-th index. So for every index i you will have to count the product of
L[X][i-1] * R[X][i+1], where i is the range [1, n-2] and X will be from 'a' to 'z'
Below is the implementation of this 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.
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Count the number of subsequences of length k having equal LCM and HCF
- Number of K length subsequences with minimum sum
- Number of subsequences of maximum length K containing no repeated elements
- Number of strings of length N with no palindromic sub string
- Find Nth even length palindromic number formed using digits X and Y
- Find two equal subsequences of maximum length with at least one different index
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Count of possible subarrays and subsequences using given length of Array
- Sum of all subsequences of length K
- Unique subsequences of length K with given sum
- Minimize sum of smallest elements from K subsequences of length L
- Count of subsequences of length 4 in form (x, x, x+1, x+1) | Set 2
- Maximize subsequences having array elements not exceeding length of the subsequence
- Count of subsequences of length atmost K containing distinct prime elements
- Check if all the palindromic sub-strings are of odd length
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.