Count palindromic characteristics of a String


In a C++ environment, the palindrome is a characteristics where we get the same value after getting the result. Assume, there is a string denoted as S and the length is N. Now we need to run an operation on that string to find the palindromic characteristic is the number of k-palindromes in a given range.

Here is a general example of the process −

Input for the process : abba
Output by the process : 6 1 0 0
Explanation of the method:
"6" 1-palindromes numbers operation = "a", "b", "b", "a", "bb", "abba".
"1" 2-palindromes numbers operation = "bb".
Because "b" is 1-palindrome number and "bb" has both left and right parts both
are equal.
"0" 3-palindrome
and 4-palindrome.

Algorithm to count palindromic character of a string

In this possible algorithm, we are going to perform the process of which counts different palindromic characteristics of a string by using different functions which checks whether a substring str[i..j] of a given string is a palindrome or not. With this algorithm, we will build some C++ syntax to learn about the problem statement in an efficient manner.

  • Step 1 − Start the process.

  • Step 2 − Declare the input output stream.

  • Step 3 − Import the built-in classes and declared functions.

  • Step 4 − Declare a for loop and iterate the process.

  • Step 5 − If the condition is satisfied then print true.

  • Step 6 − Or Else, print false.

  • Step 7 − Append the elements.

  • Step 8 − Remove the last elements.

  • Step 9 − Check difference.

  • Step 10 − Get the result and terminate the process.

Syntax to count palindromic character of a string

Initial Values : i = 0, j = n-1;
Given string 'str'
CountPS(i, j)
   If (j == i+1)
      return str[i] == str[j]
   Else if(i == j || i > j) return 0;
   Else If str[i..j] is Palindrome Number
      return countPS(i+1, j) + countPS(i, j-1) + 1 -
   countPS(i+1, j-1);
   Else
      return countPS(i+1, j) + countPS(i, j-1) -
         countPS(i+1 , j-1);

In this possible syntax above, we have tried to show you how to perform the process which counts different palindromic characteristics of a string by using different functions which checks whether a substring str[i..j] of a given string is a palindrome or not. By using this syntax we are heading towards some well known approaches to solve the problem statement in an efficient way.

Approaches to Follow

  • Approach 1 − Count palindromic characteristics of a String by using countKPalindromes(), memset(), and 2D matrix in a C++ environment.

  • Approach 2 − C++ program to find palindromic substrings of a string by using the recursive function with odd and even characters, boolean string and Overlapping Subproblems

Approach 1: Use of CountKPalindromes(), Memset(), and 2D Matrix

Use of CountKPalindromes() Method

In this possible approach, we are going to apply the countKPalindromes() method to count palindromic characteristics of a String.

int n = temp.length();
for (int i = 0; i < n/ 2; i++) {
   if (temp[i] != temp[n - i - 1]) {
      return false;
   }
}
return true;
int main(){
string s;
cin>>s;
int mxlength = 0;
int n = s.length();
for(int i=0;i<n;i++){
   for(int j=0;j<n;j++){
      string temp = s.substr(i,j-i+1);
      if(isPalindrome(temp)){
         int len = j-i+1;
         mxlength = max(mxlength,len);
      }
   }
}
int cnt=0;
for(int i=0;i<=n-mxlength;i++){
   string temp = s.substr(i,mxlength);
   if(isPalindrome(temp)){
      cnt++;
   }
}
cout<<mxlength<<" "<<cnt<<endl;

Example

//A C++ program which counts different palindromic characteristics of a string by using a function which checks whether a substring str[i..j] of a given string is a palindrome or not.
#include <bits/stdc++.h>
using namespace std;
const int MAX_STR_LEN = 1000;
bool P[MAX_STR_LEN][MAX_STR_LEN];
int Kpal[MAX_STR_LEN];
void checkSubStrPal(string str, int n){
   memset(P, false, sizeof(P));
   for (int i = 0; i < n; i++)
   P[i][i] = true;
   for (int i = 0; i < n - 1; i++)
   if (str[i] == str[i + 1])
   P[i][i + 1] = true;
   for (int gap = 2; gap < n; gap++){
      for (int i = 0; i < n - gap; i++){
         int j = gap + i;
         if (str[i] == str[j] && P[i + 1][j - 1])
         P[i][j] = true;
      }
   }
}
void countKPalindromes(int i, int j, int k){
   if (i == j){
      Kpal[k]++;
      return;
   }
   if (P[i][j] == false)
   return;
   Kpal[k]++;
   int mid = (i + j) / 2;
   if ((j - i + 1) % 2 == 1)
   mid--;
   countKPalindromes(i, mid, k + 1);
}
void printKPalindromes(string s){
   memset(Kpal, 0, sizeof(Kpal));
   int n = s.length();
   checkSubStrPal(s, n);
   for (int i = 0; i < n; i++)
   for (int j = 0; j < n - i; j++)
   countKPalindromes(j, j + i, 1);
   for (int i = 1; i <= n; i++)
   cout << Kpal[i] << " ";
   cout << "\n";
}
int main(){
   string s = "abacaba";
   printKPalindromes(s);
   return 0;
}

Output

12 4 1 0 0 0 0

Use of Memset() Method

In this possible approach, we are going to apply the memset() method to count palindromic characteristics of a String.

Example

// C++ program to find palindromic substrings of a string which returns total number of palindrome substring of length greater than equal to 2
#include <bits/stdc++.h>
using namespace std;
int CountPS(char str[], int n){
   int ans=0;
   bool P[n][n];
   memset(P, false, sizeof(P));
   for (int i = 0; i < n; i++){
      P[i][i] = true;
   }
   for (int gap = 2; gap <=n; gap++){
      for (int i = 0; i <= n-gap; i++){
         int j = gap + i-1;
         if(i==j-1){
            P[i][j]=(str[i]==str[j]);
         } else{
            P[i][j]=(str[i]==str[j] && P[i+1][j-1]);
         }
         if(P[i][j]){
            ans++;
         }
      }
   }
   return ans;
}
int main(){
   char str[] = "abaab";
   int n = strlen(str);
   cout << CountPS(str, n) << endl;
   return 0;
}

Output

3

Use of a 2D Matrix

In this possible approach, we are going to construct and apply the 2D matrix to count the palindromic characteristics of a String.

Example

//C++ program to find palindromic substrings of a string by using a 2D matrix
#include <bits/stdc++.h>
using namespace std;
int dp[1001][1001];
bool isPal(string s, int i, int j){
   if (i > j)
   return 1;
   if (dp[i][j] != -1)
   return dp[i][j];
   if (s[i] != s[j])
   return dp[i][j] = 0;
   return dp[i][j] = isPal(s, i + 1, j - 1);
}
int countSubstrings(string s){
   memset(dp, -1, sizeof(dp));
   int n = s.length();
   int count = 0;
   for (int i = 0; i < n; i++){
      for (int j = i + 1; j < n; j++){
         if (isPal(s, i, j))
         count++;
      }
   }
   return count;
}
int main(){
   string s = "abbaeae";
   cout << countSubstrings(s);
   return 0;
}

Output

4

Approach 2: Using the Recursive Function With odd and Even Characters, Boolean String and Overlapping Subproblems

Use of the Boolean String Method

In this possible approach, we are going to construct and apply the bollean strings to count the palindromic characteristics of a String.

int n = temp.length();
for (int i = 0; i < n/ 2; i++) {
   if (temp[i] != temp[n - i - 1]) {
      return false;
   }
}
return true;
}
int maxLengthOfPalindromicSubstring(string s) {
   int length = 0;
   int len = 0;
   if (s.size() == 2 and s[0] == s[1]) {
      return 2;
   } else if (s.size() < 3) {
      string c(1, s[0]);
      return 1;
   }
   string temp;
   for (int i = 0; i < s.size() - 1; i++) {
      len = 0;
      while ((i-len) >= 0 and (i+1+len) < s.size() and s[i-len] ==
      s[i+1+len]) {
         if (length < 2*len+2){
            length = 2*len+2;
            temp = s.substr(i-len, length);
         }
         len++;
      }
      len = 0;
      while ((i-len >= 0) and (i+len < s.size()) and s[i-len] == s[i+len]) {
         if (length < 2*len+1) {
            length = 2*len+1;
            temp = s.substr(i-len, length);
         }
         len++;
      }
   }
   return temp.length();
}
int main(){
string s;
cin>>s;
int n = s.length();
int mxlength = maxLengthOfPalindromicSubstring(s);
int cnt=0;
for(int i=0;i<=n-mxlength;i++){
   string temp = s.substr(i,mxlength);
   if(isPalindrome(temp)){
      cnt++;
   }
}
cout<<mxlength<<" "<<cnt<<endl;

Example

//C++ program to find palindromic substrings of a string by using the boolean string
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string s){
   for (int i = 0; i < s.length(); ++i) {
      if (s[i] != s[s.length() - i - 1]) {
         return false;
      }
   }
   return true;
}
bool ans(string s){
   string s2 = s;
   for (int i = 0; i < s.length(); ++i){
      s2 = s2.back() + s2;
      s2.pop_back();
      if (s != s2 && isPalindrome(s2)) {
         return true;
      }
   }
   return false;
}
int solve(string s){
   if (s.length() <= 3) {
      return -1;
   }
   int cnt[25] = {};
   for (int i = 0; i < s.length(); i++) {
      cnt[s[i] - 'a']++;
   }
   if (*max_element(cnt, cnt + 25) >= (s.length() - 1)) {
      return -1;
   } else {
      return (ans(s) ? 1 : 2);
   }
}
int main(){
   string s = "arbrdd";
   cout << solve(s);
   return 0;
}

Output

2

Use of the Recursive Function With odd and Even Characters Method

In this possible approach, we are going to construct and apply a string by using recursive function with odd and even characters to count the palindromic characteristics.

Example

//C++ program to find palindromic substrings of a string by using recursive function with odd and even characters
#include <bits/stdc++.h>
using namespace std;
int solveEven(string s){
   if (s.length() % 2 == 1)
   return 2;
   string ls = s.substr(0, s.length() / 2);
   string rs = s.substr(s.length() / 2, s.length());
   if (ls != rs)
   return 1;
   return solveEven(ls);
}
int solveOdd(string s){
   return 2;
}
int solve(string s){
   if (s.length() <= 3) {
      return -1;
   }
   int cnt[25] = {};
   for (int i = 0; i < s.length(); i++){
      cnt[s[i] - 'a']++;
   }
   if (*max_element(cnt, cnt + 25) >= s.length() - 1){
      return -1;
   }
   if (s.length() % 2 == 0)
   return solveEven(s);
   if (s.length() % 2 == 1)
   return solveOdd(s);
}
int main(){
   string s = "ARBRDD";
   cout << solve(s);
   return 0;
}

Output

1

Use of the Overlapping Subproblems Method

In this possible approach, we are going to apply a string by using overlapping subproblem to count the palindromic characteristics in a C++ environment.

Example

//C++ program to find palindromic substrings of a string by using Overlapping Subproblems
#include <cstring>
#include <iostream>
using namespace std;
int countPS(string str){
   int N = str.length();
   int cps[N + 1][N + 1];
   memset(cps, 0, sizeof(cps));
   for (int i = 0; i < N; i++)
   cps[i][i] = 1;
   for (int L = 2; L <= N; L++) {
      for (int i = 0; i <= N-L; i++) {
         int k = L + i - 1;
         if (str[i] == str[k])
         cps[i][k]
         = cps[i][k - 1] + cps[i + 1][k] + 1;
         else
         cps[i][k] = cps[i][k - 1] + cps[i + 1][k]
         - cps[i + 1][k - 1];
      }
   }
   return cps[0][N - 1];
}
int main(){
   string str = "arbrdd";
   cout << "Total palindromic character sequence are present here : "
   << countPS(str) << endl;
   return 0;
}

Output

Total palindromic character sequence are present here is : 9

Conclusion

Today in this article we have learned about how to implement the process to count different palindromic characteristics of a string by using a function which checks whether a substring str[i..j] of a given string is a palindrome or not in a C++ environment. With the above mentioned logics, syntax and algoritm; we have tried to build some C++ codes to solve the problem statement in an efficient manner.

Updated on: 27-Dec-2023

15 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements