Check whether two strings can be made equal by copying their characters with the adjacent ones | C ++ implementation of the approach ; Function that returns true if both the strings can be made equal with the given operation ; Lengths of both the strings have to be equal ; To store the frequency of the characters of str1 ; For every character of str2 ; If current character of str2 also appears in str1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  26 NEW_LINE bool canBeMadeEqual ( string str1 , string str2 ) { int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; if ( len1 == len2 ) { int freq [ MAX ] ; for ( int i = 0 ; i < len1 ; i ++ ) { freq [ str1 [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < len2 ; i ++ ) { if ( freq [ str2 [ i ] - ' a ' ] > 0 ) return true ; } } return false ; } int main ( ) { string str1 = " abc " , str2 = " defa " ; if ( canBeMadeEqual ( str1 , str2 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }