Check if two given strings are isomorphic to each other | C # program to check if two strings are isomorphic ; Function returns true if str1 and str2 are isomorphic ; Length of both strings must be same for one to one corresponance ; To mark visited characters in str2 ; To store mapping of every character from str1 to that of str2 and Initialize all entries of map as - 1. ; Process all characters one by on ; If current character of str1 is seen first time in it . ; If current character of str2 is already seen , one to one mapping not possible ; Mark current character of str2 as visited ; Store mapping of current characters ; If this is not first appearance of current character in str1 , then check if previous appearance mapped to same character of str2 ; Driver code
using System ; class GFG { static int size = 256 ; static bool areIsomorphic ( String str1 , String str2 ) { int m = str1 . Length ; int n = str2 . Length ; if ( m != n ) return false ; bool [ ] marked = new bool [ size ] ; for ( int i = 0 ; i < size ; i ++ ) marked [ i ] = false ; int [ ] map = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) map [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( map [ str1 [ i ] ] == - 1 ) { if ( marked [ str2 [ i ] ] == true ) return false ; marked [ str2 [ i ] ] = true ; map [ str1 [ i ] ] = str2 [ i ] ; } else if ( map [ str1 [ i ] ] != str2 [ i ] ) return false ; } return true ; } public static void Main ( ) { bool res = areIsomorphic ( " aab " , " xxy " ) ; Console . WriteLine ( res ) ; res = areIsomorphic ( " aab " , " xyz " ) ; Console . WriteLine ( res ) ; } }