Total number of odd length palindrome sub-sequence around each centre
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.
Examples:
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]Base case:
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to find the total palindromic // odd length sub-sequences void solve(string& s) { int n = s.length(); // dp array to store the number of palindromic // subsequences for 0 to i-1 and j+1 to n-1 int dp[n][n]; memset(dp, 0, sizeof dp); // We will start with the largest // distance between i and j for (int len = n - 1; len >= 0; --len) { // For each len, we fix our i for (int i = 0; i + len < n; ++i) { // For this i we will find our j int j = i + len; // Base cases if (i == 0 and j == n - 1) { if (s[i] == s[j]) dp[i][j] = 2; else if (s[i] != s[j]) dp[i][j] = 1; } else { if (s[i] == s[j]) { // If the characters are equal // then look for out of bound index if (i - 1 >= 0) { dp[i][j] += dp[i - 1][j]; } if (j + 1 <= n - 1) { dp[i][j] += dp[i][j + 1]; } if (i - 1 < 0 or j + 1 >= n) { // We have only 1 way that is to // just pick these characters dp[i][j] += 1; } } else if (s[i] != s[j]) { // If the characters are not equal if (i - 1 >= 0) { dp[i][j] += dp[i - 1][j]; } if (j + 1 <= n - 1) { dp[i][j] += dp[i][j + 1]; } if (i - 1 >= 0 and j + 1 <= n - 1) { // Substract it as we have // counted it twice dp[i][j] -= dp[i - 1][j + 1]; } } } } } vector<int> ways; for (int i = 0; i < n; ++i) { if (i == 0 or i == n - 1) { // We have just 1 palindrome // sequence of length 1 ways.push_back(1); } else { // Else total ways would be sum of dp[i-1][i+1], // that is number of palindrome sub-sequences // from 1 to i-1 + number of palindrome // sub-sequences from i+1 to n-1 int total = dp[i - 1][i + 1]; ways.push_back(total); } } for (int i = 0; i < ways.size(); ++i) { cout << ways[i] << " "; } } // Driver code int main() { string s = "xyxyx"; solve(s); return 0; } |
Java
// Java implemenation of above approach import java.util.*; class GFG { // Function to find the total palindromic // odd length sub-sequences static void solve(char[] s) { int n = s.length; // dp array to store the number of palindromic // subsequences for 0 to i-1 and j+1 to n-1 int [][]dp = new int[n][n]; // We will start with the largest // distance between i and j for (int len = n - 1; len >= 0; --len) { // For each len, we fix our i for (int i = 0; i + len < n; ++i) { // For this i we will find our j int j = i + len; // Base cases if (i == 0 && j == n - 1) { if (s[i] == s[j]) dp[i][j] = 2; else if (s[i] != s[j]) dp[i][j] = 1; } else { if (s[i] == s[j]) { // If the characters are equal // then look for out of bound index if (i - 1 >= 0) { dp[i][j] += dp[i - 1][j]; } if (j + 1 <= n - 1) { dp[i][j] += dp[i][j + 1]; } if (i - 1 < 0 || j + 1 >= n) { // We have only 1 way that is to // just pick these characters dp[i][j] += 1; } } else if (s[i] != s[j]) { // If the characters are not equal if (i - 1 >= 0) { dp[i][j] += dp[i - 1][j]; } if (j + 1 <= n - 1) { dp[i][j] += dp[i][j + 1]; } if (i - 1 >= 0 && j + 1 <= n - 1) { // Substract it as we have // counted it twice dp[i][j] -= dp[i - 1][j + 1]; } } } } } Vector<Integer> ways = new Vector<>(); for (int i = 0; i < n; ++i) { if (i == 0 || i == n - 1) { // We have just 1 palindrome // sequence of length 1 ways.add(1); } else { // Else total ways would be sum of dp[i-1][i+1], // that is number of palindrome sub-sequences // from 1 to i-1 + number of palindrome // sub-sequences from i+1 to n-1 int total = dp[i - 1][i + 1]; ways.add(total); } } for (int i = 0; i < ways.size(); ++i) { System.out.print(ways.get(i) + " "); } } // Driver code public static void main(String[] args) { char[] s = "xyxyx".toCharArray(); solve(s); } } // This code has been contributed by 29AjayKumar |
Python3
# Function to find the total palindromic # odd Length sub-sequences def solve(s): n = len(s) # dp array to store the number of palindromic # subsequences for 0 to i-1 and j+1 to n-1 dp=[[0 for i in range(n)] for i in range(n)] # We will start with the largest # distance between i and j for Len in range(n-1,-1,-1): # For each Len, we fix our i for i in range(n): if i + Len >= n: break # For this i we will find our j j = i + Len # Base cases if (i == 0 and j == n - 1): if (s[i] == s[j]): dp[i][j] = 2 elif (s[i] != s[j]): dp[i][j] = 1 else: if (s[i] == s[j]): # If the characters are equal # then look for out of bound index if (i - 1 >= 0): dp[i][j] += dp[i - 1][j] if (j + 1 <= n - 1): dp[i][j] += dp[i][j + 1] if (i - 1 < 0 or j + 1 >= n): # We have only 1 way that is to # just pick these characters dp[i][j] += 1 elif (s[i] != s[j]): # If the characters are not equal if (i - 1 >= 0): dp[i][j] += dp[i - 1][j] if (j + 1 <= n - 1): dp[i][j] += dp[i][j + 1] if (i - 1 >= 0 and j + 1 <= n - 1): # Substract it as we have # counted it twice dp[i][j] -= dp[i - 1][j + 1] ways = [] for i in range(n): if (i == 0 or i == n - 1): # We have just 1 palindrome # sequence of Length 1 ways.append(1) else: # Else total ways would be sum of dp[i-1][i+1], # that is number of palindrome sub-sequences # from 1 to i-1 + number of palindrome # sub-sequences from i+1 to n-1 total = dp[i - 1][i + 1] ways.append(total) for i in ways: print(i,end=" ") # Driver code s = "xyxyx"solve(s) # This code is contributed by mohit kumar 29 |
C#
// C# implemenation of above approach using System; using System.Collections.Generic; class GFG { // Function to find the total palindromic // odd length sub-sequences static void solve(char[] s) { int n = s.Length; // dp array to store the number of palindromic // subsequences for 0 to i-1 and j+1 to n-1 int [,]dp = new int[n, n]; // We will start with the largest // distance between i and j for (int len = n - 1; len >= 0; --len) { // For each len, we fix our i for (int i = 0; i + len < n; ++i) { // For this i we will find our j int j = i + len; // Base cases if (i == 0 && j == n - 1) { if (s[i] == s[j]) dp[i, j] = 2; else if (s[i] != s[j]) dp[i, j] = 1; } else { if (s[i] == s[j]) { // If the characters are equal // then look for out of bound index if (i - 1 >= 0) { dp[i, j] += dp[i - 1, j]; } if (j + 1 <= n - 1) { dp[i, j] += dp[i, j + 1]; } if (i - 1 < 0 || j + 1 >= n) { // We have only 1 way that is to // just pick these characters dp[i, j] += 1; } } else if (s[i] != s[j]) { // If the characters are not equal if (i - 1 >= 0) { dp[i, j] += dp[i - 1, j]; } if (j + 1 <= n - 1) { dp[i, j] += dp[i, j + 1]; } if (i - 1 >= 0 && j + 1 <= n - 1) { // Substract it as we have // counted it twice dp[i, j] -= dp[i - 1, j + 1]; } } } } } List<int> ways = new List<int>(); for (int i = 0; i < n; ++i) { if (i == 0 || i == n - 1) { // We have just 1 palindrome // sequence of length 1 ways.Add(1); } else { // Else total ways would be sum of dp[i-1][i+1], // that is number of palindrome sub-sequences // from 1 to i-1 + number of palindrome // sub-sequences from i+1 to n-1 int total = dp[i - 1,i + 1]; ways.Add(total); } } for (int i = 0; i < ways.Capacity; ++i) { Console.Write(ways[i] + " "); } } // Driver code public static void Main() { char[] s = "xyxyx".ToCharArray(); solve(s); } } // This code is contributed by AnkitRai01 |
1 3 4 3 1
Recommended Posts:
- Number of balanced bracket subsequence of length 2 and 4
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Longest palindrome subsequence with O(n) space
- Nth Even length Palindrome
- Sum of first K even-length Palindrome numbers
- Maximum Sum Subsequence of length k
- Subsequence X of length K such that gcd(X[0], X[1]) + (X[2], X[3]) + ... is maximized
- Repeated subsequence of length 2 or more
- Length of the longest substring that do not contain any palindrome
- Length of Longest Balanced Subsequence
- Maximum length subsequence possible of the form R^N K^N
- Maximum Bitwise AND value of subsequence of length K
- Count subsequence of length three in a given string
- Binary String of given length that without a palindrome of size 3
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



