# Count of Palindromic substrings in an Index range in C++

C++Server Side ProgrammingProgramming

We are given a string and a range starting from start till end and the task is to calculate the count of palindromic substring present in a given range. Palindrome strings are those strings which are similar from forward and backward of a string like nitin, aba, etc.

## For Example

Input - InputString = "cccaabbbdee", start = 2, end = 6;

Output - Count of Palindromic substrings in an Index range 7

Explanation - we are given with a range and a string so, we will start traversing the string from the start pointer which is 2 i.e. 'c' till 6 i.e. 'b' therefore the substring is 'caabb'. So the palindromic substring is 'c', 'a', 'a', 'b' , 'b', 'aa' and 'bb'. So, the count of palindromic substring is 7.

Input - InputString = "lioaabbbdee", start = 0, end = 2;

Output - Count of Palindromic substrings in an Index range 3

Explanation - we are given with a range and a string so, we will start traversing the string from the start pointer which is 0 i.e. 'l' till 2 i.e. 'o' therefore the substring is 'lio'. So the palindromic substring is 'l', 'i' and 'o'. So, the count of palindromic substring is 3.

## Approach used in the below program is as follows

• Declare a string of any given size and a range starting from a variable start till end.
• Pass the data to the function named palindrome_index(arr, InputString) for further processing
• Inside the function, declare another array named checked with the array size.
• Start Loop FOR i from 0 till the length of an array
• Inside the loop, start another Loop FOR j from 0 till the length of an array
• Inside the loop, set check as check[i][j] = 0 and arr[i][j] = 0
• Start loop FOR i from length - 1 till i is greater than 0
• Inside the loop, set check and arr of i as 1 then start another loop FOR j from i + 1 till the length of an array
• Inside the loop, check IF string at i i equals to string at j AND i + 1 is greater than j - 1 OR check[i + 1][j - 1]) not equals to 0 then set check[i][j] as 1 ELSE, set check[i][j] as 0 then set  arr[i][j] = arr[i][j - 1] + arr[i + 1][j] - arr[i + 1][j - 1] + check[i][j]
• Print a 2-D array as start and end.

## Example

import java.io.*;
class testqwe {
static void palindrome_index(int arr[][], String s) {
int length = s.length();
int[][] check = new int[length + 1][length + 1];
for (int i = 0; i <= length; i++) {
for (int j = 0; j <= length; j++) {
check[i][j] = 0;
arr[i][j] = 0;
}
}

for (int i = length - 1; i >= 0; i--) {
check[i][i] = arr[i][i] = 1;
for (int j = i + 1; j < length; j++) {
if(s.charAt(i) == s.charAt(j) && (i + 1 > j - 1 || (check[i + 1][j - 1]) != 0)) {
check[i][j] =1;
} else {
check[i][j] =0;
}
arr[i][j] = arr[i][j - 1] + arr[i + 1][j] - arr[i + 1][j - 1] + check[i][j];
}
}
}
public static void main(String args[]) {
String InputString = "cccaabbbdee";
int[][] arr;
arr = new int[50][50];
palindrome_index(arr, InputString);
int start = 2;
int end = 6;
System.out.println("Count of Palindromic substrings in an Index range " + arr[start][end]);
}
}

If we run the above code it will generate the following output −

## Output

Count of Palindromic substrings in an Index range 7
Published on 29-Jan-2021 07:45:18