
- 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
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
#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
#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!
- Related Articles
- Python program to check if a string is palindrome or not
- C Program to check if an Array is Palindrome or not
- C# program to check if a string is palindrome or not
- Swift Program to check if an Array is Palindrome or not
- Recursive program to check if number is palindrome or not in C++
- Palindrome Substring Queries in C++
- C Program to check if an array is palindrome or not using Recursion
- Write a C# program to check if a number is Palindrome or not
- Program to check if an Array is Palindrome or not using STL in C++
- Program to check a string is palindrome or not in Python
- Check if number is palindrome or not in Octal in Python
- C++ Program to Check Whether a Number is Palindrome or Not
- Golang Program to check if the binary representation of a number is palindrome or not
- Program to check string is palindrome or not with equivalent pairs in Python
- Program to check string is palindrome with lowercase characters or not in Python
