Check whether two strings can be made equal by copying their characters with the adjacent ones | JavaScript 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
let MAX = 26 ; function canBeMadeEqual ( str1 , str2 ) { let len1 = str1 . length ; let len2 = str2 . length ; if ( len1 == len2 ) { let freq = new Array ( MAX ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < len1 ; i ++ ) { freq [ str1 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; } for ( let i = 0 ; i < len2 ; i ++ ) { if ( freq [ str2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] > 0 ) return true ; } } return false ; } let str1 = " " , str2 = " " ; if ( canBeMadeEqual ( str1 , str2 ) ) document . write ( " " ) ; else document . write ( " " ) ;