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’
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 :
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
- A Program to check if strings are rotations of each other or not
- Check for balanced parentheses in an expression
- Check whether two strings are anagram of each other
- Print all interleavings of given two strings
- Check whether a given string is an interleaving of two other given strings
- Find if a string is interleaved of two other strings | DP-33
- Check if a given string is a rotation of a palindrome
- Find if an array of strings can be chained to form a circle | Set 1
- Given two strings, find if first string is a subsequence of second
- Check a given sentence for a given set of simple grammer rules
- Print all possible strings that can be made by placing spaces
- Find all distinct palindromic sub-strings of a given string
- Check if a given sequence of moves for a robot is circular or not
- Check if edit distance between two strings is one
- Find all strings formed from characters mapped to digits of a number
Improved By : Sam007