Given a string str, the task is to find the number of odd length palindromic sub-sequences around of str with str[i] as centre i.e. every index will be considered as the centre one by one.
Input: str = “xyzx”
Output: 1 2 2 1
For index 0: There is only a single sub-sequence possible i.e. “x”
For index 1: Two sub-sequences are possible i.e. “y” and “xyx”
For index 2: “z” and “xzx”
For index 3: “x”
Input: str = “aaaa”
Output: 1 3 3 1
Approach: We will use dynamic programming to solve this problem. Let’s denote length of the sting str be N. Now, Let dp[i][j] denote the number of palindromic sub-sequences from 0 to i – 1 and number of palindromic sub-sequences from j to N – 1.
Let len be the distance between i and j. For each length len, we will fix our i and j, and check whether characters str[i] and str[j] are equal or not. Then according to it, we will make our dp transitions.
If str[i] != str[j] then dp[i][j] = dp[i – 1][j] + dp[i][j + 1] – dp[i – 1][j + 1]
If str[i] == str[j] then dp[i][j] = dp[i – 1][j] + dp[i][j + 1]
If i == 0 and j == n – 1 then dp[i][j] = 2 if str[i] == str[j] else dp[i][j] = 1.
Below is the implementation of the above approach:
1 3 4 3 1
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.
- Length of longest subsequence having sum of digits of each element as a Composite Number
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Check if a subsequence of length K with odd sum exists
- Length of longest subsequence whose XOR value is odd
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Longest palindrome subsequence with O(n) space
- Total length of string from given Array of strings composed using given characters
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Minimum changes required to make each path in a matrix palindrome
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Find the equal pairs of subsequence of S and subsequence of T
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.