Check if substrings from three given strings can be concatenated to form a palindrome | C ++ program for the above approach ; Function to check if substrings from three given strings can be concatenated to form a palindrome ; Mask for S1 and S2 ; Set ( i - ' a ' ) th bit in maskA ; Set ( i - ' a ' ) th bit in maskC ; If the bitwise AND is > 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string make_palindrome ( string S1 , string S2 , string S3 ) { int maskA = 0 , maskC = 0 ; for ( char i : S1 ) maskA |= ( 1 << ( i - ' a ' ) ) ; for ( char i : S3 ) maskC |= ( 1 << ( i - ' a ' ) ) ; if ( ( maskA & maskC ) > 0 ) return " YES " ; return " NO " ; } int main ( ) { string S1 = " adcb " , S2 = " bcdb " , S3 = " abe " ; cout << make_palindrome ( S1 , S2 , S3 ) ; }