Given a string **S** of length n and a positive integer k. The task is to find number of Palindromic Subsequences of length k.

Examples:

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:

## C++

// CPP program to count number of subsequences of // given length. #include <bits/stdc++.h> #define MAX 100 #define MAX_CHAR 26 using namespace std; // Precompute the prefix and suffix array. void precompute(string s, int n, int l[][MAX], int r[][MAX]) { l[s[0] - 'a'][0] = 1; // Precompute the prefix 2D array for (int i = 1; i < n; i++) { for (int j = 0; j < MAX_CHAR; j++) l[j][i] += l[j][i - 1]; l[s[i] - 'a'][i]++; } r[s[n - 1] - 'a'][n - 1] = 1; // Precompute the Suffix 2D array. for (int i = n - 2; i >= 0; i--) { for (int j = 0; j < MAX_CHAR; j++) r[j][i] += r[j][i + 1]; r[s[i] - 'a'][i]++; } } // Find the number of palindromic subsequence of // length k int countPalindromes(int k, int n, int l[][MAX], int r[][MAX]) { int ans = 0; // If k is 1. if (k == 1) { for (int i = 0; i < MAX_CHAR; i++) ans += l[i][n - 1]; return ans; } // If k is 2 if (k == 2) { // Adding all the products of prefix array for (int i = 0; i < MAX_CHAR; i++) ans += ((l[i][n - 1] * (l[i][n - 1] - 1)) / 2); return ans; } // For k greater than 2. Adding all the products // of value of prefix and suffix array. for (int i = 1; i < n - 1; i++) for (int j = 0; j < MAX_CHAR; j++) ans += l[j][i - 1] * r[j][i + 1]; return ans; } // Driven Program int main() { string s = "aabab"; int k = 2; int n = s.length(); int l[MAX_CHAR][MAX] = { 0 }, r[MAX_CHAR][MAX] = { 0 }; precompute(s, n, l, r); cout << countPalindromes(k, n, l, r) << endl; return 0; }

## Python3

# Python3 program to count number of # subsequences of given length. MAX = 100 MAX_CHAR = 26 # Precompute the prefix and suffix array. def precompute(s, n, l, r): l[ord(s[0]) - ord('a')][0] = 1 # Precompute the prefix 2D array for i in range(1, n): for j in range(MAX_CHAR): l[j][i] += l[j][i - 1] l[ord(s[i]) - ord('a')][i] += 1 r[ord(s[n - 1]) - ord('a')][n - 1] = 1 # Precompute the Suffix 2D array. k = n - 2 while(k >= 0): for j in range(MAX_CHAR): r[j][k] += r[j][k + 1] r[ord(s[k]) - ord('a')][k] += 1 k -= 1 # Find the number of palindromic # subsequence of length k def countPalindromes(k, n, l, r): ans = 0 # If k is 1. if (k == 1): for i in range(MAX_CHAR): ans += l[i][n - 1] return ans # If k is 2 if (k == 2): # Adding all the products of # prefix array for i in range(MAX_CHAR): ans += ((l[i][n - 1] * (l[i][n - 1] - 1)) / 2) return ans # For k greater than 2. Adding all # the products of value of prefix # and suffix array. for i in range(1, n - 1): for j in range(MAX_CHAR): ans += l[j][i - 1] * r[j][i + 1] return ans # Driven Program s = "aabab" k = 2 n = len(s) l = [[0 for x in range(MAX)] for y in range(MAX_CHAR)] r = [[0 for x in range(MAX)] for y in range(MAX_CHAR)] precompute(s, n, l, r) print (countPalindromes(k, n, l, r)) # This code is written by Sachin Bisht

Output:

4

This article is contributed by **anuj0503**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.