
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count pairs of non-overlapping palindromic sub-strings of the given string in C++
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
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
- Related Articles
- Find all distinct palindromic sub-strings of a given String in Python
- Find all palindromic sub-strings of a given string - Set 2 in Python
- Count of sub-strings of length n possible from the given string in C++
- Find the count of palindromic sub-string of a string in its sorted form in Python
- Check if all the palindromic sub-strings are of odd lengths in Python
- Count the pairs of vowels in the given string in C++
- Count all Palindrome Sub-Strings in a String in C++
- Count all Palindromic Subsequence in a given String in C++
- Check if a string contains a palindromic sub-string of even length in Python
- Check if a string contains a palindromic sub-string of even length in C++
- Queries to find the last non-repeating character in the sub-string of a given string in C++
- Python program to count the pairs of reverse strings
- Maximum sum two non-overlapping subarrays of given size in C++
- Find the count of sub-strings whose characters can be rearranged to form the given word in Python
- Print all the palindromic permutations of given string in alphabetic order in C++

Advertisements