Check if two given strings are isomorphic to each other | C # 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
using System ; class GFG { static int CHAR = 26 ; static bool isoMorphic ( string str1 , string str2 ) { int n = str1 . Length ; int m = str2 . Length ; if ( n != m ) return false ; int [ ] countChars1 = new int [ CHAR ] ; int [ ] countChars2 = new int [ CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { countChars1 [ str1 [ i ] - ' a ' ] ++ ; countChars2 [ str2 [ i ] - ' a ' ] ++ ; if ( countChars1 [ str1 [ i ] - ' a ' ] != countChars2 [ str2 [ i ] - ' a ' ] ) { return false ; } } return true ; } public static void Main ( string [ ] args ) { Console . WriteLine ( isoMorphic ( " aab " , " xxy " ) ? 1 : 0 ) ; Console . WriteLine ( isoMorphic ( " aab " , " xyz " ) ? 1 : 0 ) ; } }