Queries to check if substring[L…R] is palindrome or not in C++ Program


In this problem, we are given string str, Q number of queries each consisting of two values L and R for substring[L...R]. Our task is to create a program to solve Queries to check if substring[L…R] is palindrome or not.

Problem Description − For solving each query, we need to check whether the substring created within the range L to R is a palindrome or not.

Let’s take an example to understand the problem,

Input

str = “abccbeba” , Q = 3
Query[][] = {{1, 4}, {0, 6}, {4, 6}}

Output

Palindrome
Not Palindrome
Palindrome

Explanation

Creating all substring for the given
substrings : Substring[1...4] = “bccb”, it is a palindrome
Substring[0...6] = “abccbeb”, it is a not palindrome
Substring[4...6] = “beb”, it is a palindrome

Solution Approach

A simple solution to the problem is to solve each query. To solve it, we need to find the substring from index range L to R. And check if the substring is a palindrome or not.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int isPallindrome(string str){
   int i, length;
   int flag = 0;
   length = str.length();
   for(i=0;i < length ;i++){
      if(str[i] != str[length-i-1]) {
         flag = 1; break;
      }
   }
   if (flag==1)
      return 1;
      return 0;
   }
   void solveAllQueries(string str, int Q, int query[][2]){
      for(int i = 0; i < Q; i++){ isPallindrome(str.substr(query[i][0] - 1, query[i][1] -       1))?cout<<"Palindrome\n":cout<<"Not palindrome!\n";
   }
}
int main() {
   string str = "abccbeba"; int Q = 3;
   int query[Q][2] = {{1, 3}, {2, 5}, {4, 5}};
   solveAllQueries(str, Q, query);
   return 0;
}

Output

Palindrome
Not palindrome!
Palindrome

This is a naive approach but is not an efficient one.

An efficient solution to the problem is using dynamic programming approach. For solving, we need to create a DP array a 2-D array that stores a boolean value that denotes whether the substring[i...j] is a palindrome for DP[i][j].

We will create this DP matrix and check for all L-R values of each query.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void computeDP(int DP[][50], string str){
   int length = str.size();
   int i, j;
   for (i = 0; i < length; i++) {
      for (j = 0; j < length; j++)
      DP[i][j] = 0;
   }
   for (j = 1; j <= length; j++) {
      for (i = 0; i <= length - j; i++) {
         if (j <= 2) {
            if (str[i] == str[i + j - 1])
            DP[i][i + j - 1] = 1;
         }
         else if (str[i] == str[i + j - 1])
         DP[i][i + j - 1] = DP[i + 1][i + j - 2];
      }
   }
}
void solveAllQueries(string str, int Q, int query[][2]){
   int DP[50][50];
   computeDP(DP, str);
   for(int i = 0; i < Q; i++){
      DP[query[i][0] - 1][query[i][1] - 1]?cout<<"not    palindrome!\n":cout<<"palindrome!\n";
   }
}
int main() {
   string str = "abccbeba"; int Q = 3;
   int query[Q][2] = {{1, 3}, {2, 5}, {4, 5}};
   solveAllQueries(str, Q, query);
   return 0;
}

Output

palindrome!
not palindrome!
palindrome!

Updated on: 22-Dec-2020

362 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements