Check whether two strings can be made equal by copying their characters with the adjacent ones | Java implementation of the above 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
class GFG { static int MAX = 26 ; static boolean canBeMadeEqual ( String str1 , String str2 ) { int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; if ( len1 == len2 ) { int freq [ ] = new int [ MAX ] ; for ( int i = 0 ; i < len1 ; i ++ ) { freq [ str1 . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < len2 ; i ++ ) { if ( freq [ str2 . charAt ( i ) - ' a ' ] > 0 ) return true ; } } return false ; } public static void main ( String [ ] args ) { String str1 = " abc " , str2 = " defa " ; if ( canBeMadeEqual ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }