Map every character of one string to another such that all occurrences are mapped to the same character | Javascript implementation of the approach ; Function that returns true if the mapping is possible ; Both the strings are of un - equal lengths ; To store the frequencies of the characters in both the string ; Update frequencies of the characters ; For every character of s1 ; If current character is not present in s1 ; Find a character in s2 that has frequency equal to the current character 's  frequency in s1 ; If such character is found ; Set the frequency to - 1 so that it doesn 't get picked again ; Set found to true ; If there is no character in s2 that could be mapped to the current character in s1 ; Driver code
var MAX = 26 ; function canBeMapped ( s1 , l1 , s2 , l2 ) { if ( l1 != l2 ) return false ; var freq1 = Array ( MAX ) . fill ( 0 ) ; var freq2 = Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < l1 ; i ++ ) freq1 [ s1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var i = 0 ; i < l2 ; i ++ ) freq2 [ s2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var i = 0 ; i < MAX ; i ++ ) { if ( freq1 [ i ] == 0 ) continue ; var found = false ; for ( var j = 0 ; j < MAX ; j ++ ) { if ( freq1 [ i ] == freq2 [ j ] ) { freq2 [ j ] = - 1 ; found = true ; break ; } } if ( ! found ) return false ; } return true ; } var s1 = " " ; var s2 = " " ; var l1 = s1 . length ; var l2 = s2 . length ; if ( canBeMapped ( s1 , l1 , s2 , l2 ) ) document . write ( " " ) ; else document . write ( " " ) ;