Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | C ++ program to implement the above approach ; iterate through the length if we could find a [ i ] = = b [ j ] we could increment I and decrement j ; else we could just break the loop as its not a palindrome type sequence ; we could concatenate the a ' s ▁ left ▁ part ▁ + b ' s right part in a variable a and a ' s ▁ right ▁ part + b ' s left in the variable b ; we would check for the palindrome condition if yes we print True else False ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string rev ( string str ) { int st = 0 ; int ed = str . length ( ) - 1 ; string s = str ; while ( st < ed ) { swap ( s [ st ] , s [ ed ] ) ; st ++ ; ed -- ; } return s ; } bool check ( string a , string b , int n ) { int i = 0 ; int j = n - 1 ; while ( i < n ) { if ( a [ i ] != b [ j ] ) break ; i += 1 ; j -= 1 ; string xa = a . substr ( i , j + 1 - i ) ; string xb = b . substr ( i , j + 1 - i ) ; if ( ( xa == rev ( xa ) ) or ( xb == rev ( xb ) ) ) return true ; } return false ; } int main ( ) { string a = " xbdef " ; string b = " cabex " ; if ( check ( a , b , a . length ( ) ) == true or check ( b , a , a . length ( ) ) == true ) cout << " True " ; else cout << " False " ; }