Check if two given strings are isomorphic to each other
Two strings str1 and str2 are called isomorphic if there is a one to one mapping possible for every character of str1 to every character of str2. And all occurrences of every character in ‘str1’ map to same character in ‘str2’.
Examples:
Input: str1 = "aab", str2 = "xxy" Output: True 'a' is mapped to 'x' and 'b' is mapped to 'y'. Input: str1 = "aab", str2 = "xyz" Output: False One occurrence of 'a' in str1 has 'x' in str2 and other occurrence of 'a' has 'y'.
We strongly recommend that you click here and practice it, before moving on to the solution.
A Simple Solution is to consider every character of ‘str1’ and check if all occurrences of it map to same character in ‘str2’. Time complexity of this solution is O(n*n).
An Efficient Solution can solve this problem in O(n) time. The idea is to create an array to store mappings of processed characters.
1) If lengths of str1 and str2 are not same, return false.
2) Do following for every character in str1 and str2
a) If this character is seen first time in str1,
then current of str2 must have not appeared before.
(i) If current character of str2 is seen, return false.
Mark current character of str2 as visited.
(ii) Store mapping of current characters.
b) Else check if previous occurrence of str1[i] mapped
to same character.Below is the implementation of above idea :
C++
// C++ program to check if two strings are isomorphic#include<bits/stdc++.h>using namespace std;#define MAX_CHARS 256// This function returns true if str1 and str2 are ismorphicbool areIsomorphic(string str1, string str2){ int m = str1.length(), n = str2.length(); // Length of both strings must be same for one to one // corresponance if (m != n) return false; // To mark visited characters in str2 bool marked[MAX_CHARS] = {false}; // To store mapping of every character from str1 to // that of str2. Initialize all entries of map as -1. int map[MAX_CHARS]; memset(map, -1, sizeof(map)); // Process all characters one by on for (int i = 0; i < n; i++) { // If current character of str1 is seen first // time in it. if (map[str1[i]] == -1) { // If current character of str2 is already // seen, one to one mapping not possible if (marked[str2[i]] == true) return false; // Mark current character of str2 as visited marked[str2[i]] = true; // Store mapping of current characters map[str1[i]] = str2[i]; } // If this is not first appearance of current // character in str1, then check if previous // appearance mapped to same character of str2 else if (map[str1[i]] != str2[i]) return false; } return true;}// Driver programint main(){ cout << areIsomorphic("aab", "xxy") << endl; cout << areIsomorphic("aab", "xyz") << endl; return 0;} |
Java
// Java program to check if two strings are isomorphicimport java.io.*;import java.util.*;class Isomorphic{ static int size = 256; // Function returns true if str1 and str2 are ismorphic static boolean areIsomorphic(String str1, String str2) { int m = str1.length(); int n = str2.length(); // Length of both strings must be same for one to one // corresponance if(m != n) return false; // To mark visited characters in str2 Boolean[] marked = new Boolean[size]; Arrays.fill(marked, Boolean.FALSE); // To store mapping of every character from str1 to // that of str2. Initialize all entries of map as -1. int[] map = new int[size]; Arrays.fill(map, -1); // Process all characters one by on for (int i = 0; i < n; i++) { // If current character of str1 is seen first // time in it. if (map[str1.charAt(i)] == -1) { // If current character of str2 is already // seen, one to one mapping not possible if (marked[str2.charAt(i)] == true) return false; // Mark current character of str2 as visited marked[str2.charAt(i)] = true; // Store mapping of current characters map[str1.charAt(i)] = str2.charAt(i); } // If this is not first appearance of current // character in str1, then check if previous // appearance mapped to same character of str2 else if (map[str1.charAt(i)] != str2.charAt(i)) return false; } return true; } // driver program public static void main (String[] args) { boolean res = areIsomorphic("aab", "xxy"); System.out.println(res); res = areIsomorphic("aab", "xyz"); System.out.println(res); }} |
Python
# Python program to check if two strings are isomorphicMAX_CHARS = 256# This function returns true if str1 and str2 are isomorphicdef areIsomorphic(string1, string2): m = len(string1) n = len(string2) # Length of both strings must be same for one to one # corresponance if m != n: return False # To mark visited characters in str2 marked = [False] * MAX_CHARS # To store mapping of every character from str1 to # that of str2. Initialize all entries of map as -1 map = [-1] * MAX_CHARS # Process all characters one by one for i in xrange(n): # if current character of str1 is seen first # time in it. if map[ord(string1[i])] == -1: # if current character of st2 is already # seen, one to one mapping not possible if marked[ord(string2[i])] == True: return False # Mark current character of str2 as visited marked[ord(string2[i])] = True # Store mapping of current characters map[ord(string1[i])] = string2[i] # If this is not first appearance of current # character in str1, then check if previous # appearance mapped to same character of str2 elif map[ord(string1[i])] != string2[i]: return False return True# Driver programprint areIsomorphic("aab","xxy")print areIsomorphic("aab","xyz")# This code is contributed by Bhavya Jain |
C#
// C# program to check if two// strings are isomorphicusing System;class GFG { static int size = 256; // Function returns true if str1 // and str2 are ismorphic static bool areIsomorphic(String str1, String str2) { int m = str1.Length; int n = str2.Length; // Length of both strings must be same // for one to one corresponance if(m != n) return false; // To mark visited characters in str2 bool[] marked = new bool[size]; for(int i = 0; i < size; i++) marked[i]= false; // To store mapping of every character // from str1 to that of str2 and // Initialize all entries of map as -1. int[] map = new int[size]; for(int i = 0; i < size; i++) map[i]= -1; // Process all characters one by on for (int i = 0; i < n; i++) { // If current character of str1 is // seen first time in it. if (map[str1[i]] == -1) { // If current character of str2 // is already seen, one to // one mapping not possible if (marked[str2[i]] == true) return false; // Mark current character of // str2 as visited marked[str2[i]] = true; // Store mapping of current characters map[str1[i]] = str2[i]; } // If this is not first appearance of current // character in str1, then check if previous // appearance mapped to same character of str2 else if (map[str1[i]] != str2[i]) return false; } return true; } // Driver code public static void Main () { bool res = areIsomorphic("aab", "xxy"); Console.WriteLine(res); res = areIsomorphic("aab", "xyz"); Console.WriteLine(res); }}// This code is contributed by Sam007. |
Javascript
<script> // Javascript program to check if two // strings are isomorphic let size = 256; // Function returns true if str1 // and str2 are ismorphic function areIsomorphic(str1, str2) { let m = str1.length; let n = str2.length; // Length of both strings must be same // for one to one corresponance if(m != n) return false; // To mark visited characters in str2 let marked = new Array(size); for(let i = 0; i < size; i++) marked[i]= false; // To store mapping of every character // from str1 to that of str2 and // Initialize all entries of map as -1. let map = new Array(size); map.fill(0); for(let i = 0; i < size; i++) map[i]= -1; // Process all characters one by on for (let i = 0; i < n; i++) { // If current character of str1 is // seen first time in it. if (map[str1[i].charCodeAt()] == -1) { // If current character of str2 // is already seen, one to // one mapping not possible if (marked[str2[i].charCodeAt()] == true) return false; // Mark current character of // str2 as visited marked[str2[i].charCodeAt()] = true; // Store mapping of current characters map[str1[i].charCodeAt()] = str2[i].charCodeAt(); } // If this is not first appearance of current // character in str1, then check if previous // appearance mapped to same character of str2 else if (map[str1[i].charCodeAt()] != str2[i].charCodeAt()) return 0; } return 1; } let res = areIsomorphic("aab", "xxy"); document.write(res + "</br>"); res = areIsomorphic("aab", "xyz"); document.write(res); // This code is contributed by decode2207.</script> |
Output:
1 0
Another Approach:
- In this approach, we will count the number of occurrences of a particular character in both the string using two arrays, while we will compare the two arrays if at any moment with the loop the count of the current character in both strings becomes different we return false, else after the loop ends we return true.
- Follow the code given below you will understand everything.
Below is the implementation of the above idea :
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;#define MAX_CHARS 256// This function returns true if// str1 and str2 are ismorphicbool areIsomorphic(string str1, string str2){ int n = str1.length(), m = str2.length(); // Length of both strings must be // same for one to one // correspondence if (n != m) return false; // For counting the previous appearances of character in // both the strings int count[MAX_CHARS] = { 0 }; int dcount[MAX_CHARS] = { 0 }; // Process all characters one by one for (int i = 0; i < n; i++) { count[str1[i]]++; dcount[str2[i]]++; // 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. if (count[str1[i]] != dcount[str2[i]]) return false; } return true;}// Driver Codeint main(){ cout << areIsomorphic("aab", "xxy") << endl; cout << areIsomorphic("aab", "xyz") << endl; return 0;} |
Java
// Java program for the above approachimport java.io.*;class GFG{static final int CHAR = 256;// This function returns true if// str1 and str2 are ismorphicstatic boolean isoMorphic(String str1, String str2){ int n = str1.length(); int m = str2.length(); // Length of both strings must be // same for one to one // correspondence if (n != m) return false; // For counting the previous appearances // of character in both the strings int[] countChars1 = new int[CHAR]; int[] countChars2 = new int[CHAR]; // Process all characters one by one for(int i = 0; i < n; i++) { countChars1[str1.charAt(i)]++; countChars2[str2.charAt(i)]++; // 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. if (countChars1[str1.charAt(i)] != countChars2[str2.charAt(i)]) { return false; } } return true;}// Driver Codepublic static void main(String[] args){ System.out.println(isoMorphic("aab", "xxy") ? 1 : 0); System.out.println(isoMorphic("aab", "xyz") ? 1 : 0);}}// This code is contributed by rohansharma1808 |
C#
// C# program for the above approachusing System;class GFG { static int CHAR = 256; // This function returns true if // str1 and str2 are ismorphic static bool isoMorphic(string str1, string str2) { int n = str1.Length; int m = str2.Length; // Length of both strings must be // same for one to one // correspondence if (n != m) return false; // For counting the previous appearances // of character in both the strings int[] countChars1 = new int[CHAR]; int[] countChars2 = new int[CHAR]; // Process all characters one by one for (int i = 0; i < n; i++) { countChars1[str1[i]]++; countChars2[str2[i]]++; // 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. if (countChars1[str1[i]] != countChars2[str2[i]]) { return false; } } return true; } // Driver Code public static void Main(string[] args) { Console.WriteLine(isoMorphic("aab", "xxy") ? 1 : 0); Console.WriteLine(isoMorphic("aab", "xyz") ? 1 : 0); }}// This code is contributed by ukasp. |
Javascript
<script> // Javascript program for the above approach let CHAR = 256; // This function returns true if // str1 and str2 are ismorphic function isoMorphic(str1, str2) { let n = str1.length; let m = str2.length; // Length of both strings must be // same for one to one // correspondence if (n != m) return false; // For counting the previous appearances // of character in both the strings let countChars1 = new Array(CHAR); countChars1.fill(0); let countChars2 = new Array(CHAR); countChars2.fill(0); // Process all characters one by one for(let i = 0; i < n; i++) { countChars1[str1[i].charCodeAt()]++; countChars2[str2[i].charCodeAt()]++; // 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. if (countChars1[str1[i].charCodeAt()] != countChars2[str2[i].charCodeAt()]) { return false; } } return true; } document.write(isoMorphic("aab", "xxy") ? 1 + "</br>" : 0 + "</br>"); document.write(isoMorphic("aab", "xyz") ? 1 : 0);</script> |
1 0
Time Complexity: O(n)
Thanks to Gaurav and Utkarsh for suggesting above approach.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.



