Check if one string can be converted to another | JavaScript implementation of the above approach . ; Function for find from Disjoint set algorithm ; Function for the union from Disjoint set algorithm ; Function to check if one String can be converted to another . ; All the characters are checked whether it 's either not replaced or replaced  by a similar character using a map. ; To check if there are cycles . If yes , then they are not convertible . Else , they are convertible . ; Function to initialize parent array for union and find algorithm . ; Driver code
var parent = new Array ( 26 ) . fill ( 0 ) ; function find ( x ) { if ( x !== parent [ x ] ) return ( parent [ x ] = find ( parent [ x ] ) ) ; return x ; } function join ( x , y ) { var px = find ( x ) ; var pz = find ( y ) ; if ( px !== pz ) { parent [ pz ] = px ; } } function convertible ( s1 , s2 ) { var mp = { } ; for ( var i = 0 ; i < s1 . length ; i ++ ) { if ( ! mp . hasOwnProperty ( s1 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) { mp [ s1 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = s2 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; } else { if ( mp [ s1 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] !== s2 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) return false ; } } for ( const [ key , value ] of Object . entries ( mp ) ) { if ( key === value ) continue ; else { if ( find ( key ) == find ( value ) ) return false ; else join ( key , value ) ; } } return true ; } function initialize ( ) { for ( var i = 0 ; i < 26 ; i ++ ) { parent [ i ] = i ; } } var s1 , s2 ; s1 = " " ; s2 = " " ; initialize ( ) ; if ( convertible ( s1 , s2 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;