Count of three non-overlapping substrings which on concatenation forms a palindrome


Introduction

In this tutorial, we will elaborate an approach for finding three non-overlapping substrings from a given string s, and when all substrings are combined together they form a palindrome. To solve this task we use the string class features of the C++ programming language.

Palindrome in a string means the string reads the same in both forward and backward directions. The palindrome string example is Madam.

Suppose there is a string "s" and the substrings are a, b, and c. When you combine a, b, and c, they form a palindrome string. Here is an example to understand the problem logic.

Statement Explanation

String s = “abbacab”      
Acceptable substrings of length 3 are: “abb”, “bac”, and “bba”.

When we concatenate all the three substrings the resulting string is a palindrome and that string is abbbacbba.

Syntax

The size() function belongs to the string class and it is used to get the size of the input string with its character length.

string_name,size();  

Algorithm

  • Take an input string.

  • Initialize a counter variable that keeps track of the number of palindromic substrings.

  • Used 3 nested for loops to generate 3 possible substrings of defined lengths.

  • The first inner loop is initialized from 0 to string length - 3.

  • The second inner loop is initialized from the first inner loop + 1 to string length - 2.

  • The outer loop is initialized from the second loop + 1 to string length - 1.

  • When all substrings are found, concatenate them.

  • Check whether substring palindromes exist, if so, increase the counter variable value.

  • Print the counter variable value.

Example

To implement the above algorithm using C++, we use an input string and generate all possible combinations of the substrings and consider only those substrings that are palindromes. The counter variable will increase if such substrings are possible. Print the result of the counter variable.

#include <bits/stdc++.h>
using namespace std;
 
// user defined function to check formed substrings are palindrome or not
bool isStringPalin(int a, int b, int c, int d, int x, int y, string st){
   int begin = a, stop = y;
   while (begin < stop) {
      if (st[begin] != st[stop])
         return false;
 
      begin++;
      if (begin == b + 1)
         begin = c;
      stop--;
      if (stop == x - 1)
         stop = d;
   }
   return true;
}
 
// User defined function to count the number of useful substrings
int countSubString(string st){
   //Counting variable to count and return the number of substrings
   int ct = 0;
   int l = st.size();
 
   //It is to select the first substring
   for (int a = 0; a < l - 2; a++) {
      for (int b = a; b < l - 2; b++){
 
         // This loop selects the second useful substring
         for (int c = b + 1; c < l - 1; c++) {
            for (int d = c; d < l - 1; d++) {
 
               // this for loop will select the third substring
               for (int x = d + 1; x < l; x++) {
                  for (int y = x; y < l; y++) {
 
                     // If condition to check the selected substrings are forming palindrome or not
                     if (isStringPalin(a, b, c, d, x, y, st)) {
                        ct++;
                     }
                  }
               }
            }
         }
      }
   }
   // returning the count variable that stores the number of useful substrings
   return ct;
}
 
// Controlling code
int main(){
   string st = "abcab";
   cout << "The possible number of substrings are: "<< countSubString(st);
 
   return 0;
}

Output

The possible number of substrings are: 4

Conclusion

We developed a method to find valid substrings that form a palindrome. To implement the solution, we used C++ loops and if conditions. To implement one of the examples using C++ we used the size() function and nested loops. The nested loop helps in finding different length substrings and the size() function returns the size of the string.

Updated on: 31-Jul-2023

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements