Count of Palindromic substrings in an Index range

4

Given a string str of small alphabetic characters other than this we will be given many substrings of this string in form of index tuples. We need to find out the count of the palindromic substrings in given substring range.
Examples:

Input : String str = "xyaabax"
           Range1 = (3, 5)   
           Range2 = (2, 3) 
Output : 4
         3
For Range1,  substring is "aba"
Count of palindromic substring in "aba" is 
four : "a", "b", "aba", "a"
For Range2,  substring is "aa"
Count of palindromic substring in "aa" is 
3 : "a", "a", "aa"

Prerequisite : Count All Palindrome Sub-Strings in a String

We can solve this problem using dynamic programming. First we will make a 2D array isPalin, isPalin[i][j] will be 1 if string(i..j) is a palindrome otherwise it will be 0. After constructing isPalin we will construct another 2D array dp, dp[i][j] will tell the count of palindromic substring in string(i..j)
Now we can write the relation among isPalin and dp values as shown below,


// isPalin[i][j] will be 1 if ith and jth characters 
// are equal and mid substring str(i+1..j-1) is also
// a palindrome
isPalin[i][j] = (str[i] == str[j]) and 
                (isPalin[i + 1][j – 1])
              
// Similar to set theory we can write the relation among
// dp values as,
// dp[i][j] will be addition of number of palindromes from 
// i to j-1 and i+1 to j  subtracting palindromes from i+1
// to j-1 because they are counted twice once in dp[i][j-1] 
// and then in dp[i + 1][j] plus 1 if str(i..j) is also a
// palindrome
dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1] + 
                                     isPalin[i][j];

Total time complexity of solution will be O(length ^ 2) for constructing dp array then O(1) per query.

C/C++

// C++ program to query number of palindromic
// substrings of a string in a range
#include <bits/stdc++.h>
using namespace std;
#define M 50

// Utility method to construct the dp array
void constructDP(int dp[M][M], string str)
{
    int l = str.length();

    // declare 2D array isPalin, isPalin[i][j] will
    // be 1 if str(i..j) is palindrome
    int isPalin[l + 1][l + 1];

    // initialize dp and isPalin array by zeros
    for (int i = 0; i <= l; i++) {
        for (int j = 0; j <= l; j++) {
            isPalin[i][j] = dp[i][j] = 0;
        }
    }

    // loop for starting index of range
    for (int i = l - 1; i >= 0; i--) {

        // initialize value for one character strings as 1
        isPalin[i][i] = 1;
        dp[i][i] = 1;

        // loop for ending index of range
        for (int j = i + 1; j < l; j++) {

            /* isPalin[i][j] will be 1 if ith and
               jth characters are equal and mid
               substring str(i+1..j-1) is also a
               palindrome             */
            isPalin[i][j] = (str[i] == str[j] &&
            (i + 1 > j - 1 || isPalin[i + 1][j - 1]));

            /* dp[i][j] will be addition of number
               of palindromes from i to j-1 and i+1
               to j subtracting palindromes from i+1
               to j-1 (as counted twice) plus 1 if
               str(i..j) is also a palindrome */
            dp[i][j] = dp[i][j - 1] + dp[i + 1][j] -
                    dp[i + 1][j - 1] + isPalin[i][j];
        }
    }
}

// method returns count of palindromic substring in range (l, r)
int countOfPalindromeInRange(int dp[M][M], int l, int r)
{
    return dp[l][r];
}

// Driver code to test above methods
int main()
{
    string str = "xyaabax";

    int dp[M][M];
    constructDP(dp, str);

    int l = 3;
    int r = 5;

    cout << countOfPalindromeInRange(dp, l, r);
    return 0;
}

Java

// Java program  to query number of palindromic
// substrings of a string in a range
import java.io.*;

class GFG
{
    // Function to construct the dp array
    static void constructDp(int dp[][], String str)
    {
        int l = str.length();
 
        // declare 2D array isPalin, isPalin[i][j] will
        // be 1 if str(i..j) is palindrome
        int[][] isPalin = new int[l + 1][l + 1];
 
        // initialize dp and isPalin array by zeros
        for (int i = 0; i <= l; i++) 
        {
            for (int j = 0; j <= l; j++) 
            {
                isPalin[i][j] = dp[i][j] = 0;
            }
        }
 
        // loop for starting index of range
        for (int i = l - 1; i >= 0; i--) 
        {
            // initialize value for one character strings as 1
            isPalin[i][i] = 1;
            dp[i][i] = 1;
 
            // loop for ending index of range
            for (int j = i + 1; j < l; j++) 
            {
                /* isPalin[i][j] will be 1 if ith and
                jth characters are equal and mid
                substring str(i+1..j-1) is also a
                palindrome             */
                isPalin[i][j] = (str.charAt(i) == str.charAt(j) &&
                        (i + 1 > j - 1 || (isPalin[i + 1][j - 1]) != 0)) ? 1 : 0;
 
                /* dp[i][j] will be addition of number
                of palindromes from i to j-1 and i+1
                to j subtracting palindromes from i+1
                to j-1 (as counted twice) plus 1 if
                str(i..j) is also a palindrome */
                dp[i][j] = dp[i][j - 1] + dp[i + 1][j] -
                        dp[i + 1][j - 1] + isPalin[i][j];
            }
        }
    }
    
    // method returns count of palindromic substring in range (l, r)
    static int countOfPalindromeInRange(int dp[][], int l, int r)
    {
        return dp[l][r];
    }
    
    // driver program
    public static void main(String args[])
    {
        int MAX = 50;
        String str = "xyaabax";
        int[][] dp = new int[MAX][MAX];
        constructDp(dp, str);
        
        int l = 3;
        int r = 5;
        System.out.println(countOfPalindromeInRange(dp, l, r));
    }
}

// Contributed by Pramod Kumar


Output:

4

This article is contributed by Utkarsh Trivedi. 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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



4 Average Difficulty : 4/5.0
Based on 5 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.