Check for Palindrome after every character replacement Query | C ++ program to find if string becomes palindrome after every query . ; Function to check if string is Palindrome or Not ; Takes two inputs for Q queries . For every query , it prints Yes if string becomes palindrome and No if not . ; Process all queries one by one ; query 1 : i1 = 3 , i2 = 0 , ch = ' e ' query 2 : i1 = 0 , i2 = 2 , ch = ' s ' replace character at index i1 & i2 with new ' ch ' ; check string is palindrome or not ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool IsPalindrome ( string & str ) { int n = strlen ( str ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) if ( str [ i ] != str [ n - 1 - i ] ) return false ; return true ; } void Query ( string & str , int Q ) { int i1 , i2 ; char ch ; for ( int q = 1 ; q <= Q ; q ++ ) { cin >> i1 >> i2 >> ch ; str [ i1 ] = str [ i2 ] = ch ; ( isPalindrome ( str ) == true ) ? cout << " YES " << endl : cout << " NO " << endl ; } } int main ( ) { char str [ ] = " geeks " ; int Q = 2 ; Query ( str , Q ) ; return 0 ; }