Check if two given strings are isomorphic to each other | Javascript program for the above approach ; This function returns true if str1 and str2 are isomorphic ; Length of both strings must be same for one to one correspondence ; For counting the previous appearances of character in both the strings ; Process all characters one by one ; For string to be isomorphic the previous counts of appearances of current character in both string must be same if it is not same we return false . ; Driver Code
let CHAR = 26 ; function isoMorphic ( str1 , str2 ) { let n = str1 . length ; let m = str2 . length ; if ( n != m ) return false ; let countChars1 = new Array ( CHAR ) ; countChars1 . fill ( 0 ) ; let countChars2 = new Array ( CHAR ) ; countChars2 . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { countChars1 [ str1 [ i ] . charCodeAt ( ) - ' ' ] ++ ; countChars2 [ str2 [ i ] . charCodeAt ( ) - ' ' ] ++ ; if ( countChars1 [ str1 [ i ] . charCodeAt ( ) - ' ' ] != countChars2 [ str2 [ i ] . charCodeAt ( ) - ' ' ] ) { return false ; } } return true ; } document . write ( isoMorphic ( " " , " " ) ? 1 + " " : 0 + " " ) ; document . write ( isoMorphic ( " " , " " ) ? 1 : 0 ) ;