Count pairs of non-overlapping palindromic sub-strings of the given string in C++

C++Server Side ProgrammingProgramming

We are given an input as a string, the task is to find out the count of pairs of non-overlapping palindromic sub-strings of the given input string. The value of arr[i][j] is true if the substring is a palindrome, otherwise false. We will take combination out of the string and check if the pairs fulfil the criteria.

Let us understand with examples.

Input: ABC

Output: Count pairs of non-overlapping palindromic sub-strings is 3

Explanation: The possible pairs are (A) (B) (C) ,(A) (BC),(AB) (C),(ABC) 

Input: ABCD

Output: Count pairs of non-overlapping palindromic sub-strings is 8

Explanation: The possible pairs are (A)(B)(C)(D),(A)(B)(CD),(A)(BC)(D),(A)(BCD),(AB)(C)(D),(AB)(CD),

(ABC)(D),(ABCD) 

Approach used in the below program is as follows

  • A string is taken as input and passed in the function pair_count(text) for further processing.
  • Initially a boolean 2D array of size 100 arr[ ][ ] is created and maintained to be filled in a bottom up approach and the input string(text) is converted to a character array.
  • The array is then calculated by checking the value arr[i+1][j-1],if the value is true and str[i] is the same as str[j], then we make arr[i][j] true. Otherwise, the value of arr[i][j] is made false.
  • After that start[ ] and end[ ] are initialised and start[i] stores the palindrome count of the number of palindromes to the left of the index(including i) and end[i] stores the palindrome count of the number of elements to the right of the index(including i)
  • A loop is then iterated from 0 to str.length() - 1 and inside the loop the result is calculated by summing up the result with the product of start[i] * end[i + 1].

Example

Live Demo

import java.io.*;
import java.util.*;
class tutorialPoint {
   static int SIZE = 100;
   static int pair_count(String str) {
      boolean arr[][] = new boolean[SIZE][SIZE];
      char[] ch = str.toCharArray();
      for (int i = 0; i < ch.length; i++) {
         for (int j = 0; j < ch.length; j++) {
            arr[i][j] = false;
         }
      }
      for (int j = 1; j <= ch.length; j++) {
         for (int i = 0; i <= ch.length - j; i++) {
            if (j <= 2) {
               if (ch[i] == ch[i + j - 1]) {
                  arr[i][i + j - 1] = true;
               }
            } else if (ch[i] == ch[i + j - 1]) {
               arr[i][i + j - 1] = arr[i + 1][i + j - 2];
            }
         }
      }
      int start[] = new int[str.length()];
      int end[] = new int[str.length()];
      start[0] = 1;
      for (int i = 1; i < str.length(); i++) {
         for (int j = 0; j <= i; j++) {
            if (arr[j][i] == true) {
               start[i]++;
            }
         }
      }
      end[str.length() - 1] = 1;
      for (int i = str.length() - 2; i >= 0; i--) {
         end[i] = end[i + 1];
         for (int j = str.length() - 1; j >= i; j--) {
            if (arr[i][j] == true) {
               end[i]++;
            }
         }
      }
      int result = 0;
      for (int i = 0; i < str.length() - 1; i++) {
         result = result + start[i] * end[i + 1];
      }
      return result;
   }

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in); //ABCD
      String text = scan.next();
      System.out.println("Count pairs of non-overlapping palindromic sub-strings is\t" + pair_count(text));
   }
}

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

Output

Count pairs of non-overlapping palindromic sub-strings is 8
raja
Published on 29-Jan-2021 08:25:37
Advertisements