Javatpoint Logo
Javatpoint Logo

Check Whether Two Strings Are Isomorphic to Each Other or Not in Python

The problem of checking whether two strings are isomorphic to each other in Python involves comparing two given strings and determining if they have a one-to-one mapping or correspondence of characters between them.

In other words, two strings are said to be isomorphic if they have the same pattern of characters, with each character in one string mapping to a corresponding character in the other string.

For example, the strings "egg" and "add" are isomorphic since the characters 'e' is mapped to 'a' and 'g' is mapped to 'd'.

The strings 'abca' and 'xywz' are not isomorphic since the character 'a' can be mapped to 'x' or 'z'.

The goal of the problem is to implement a function in Python that takes in two strings as inputs and returns True if they are isomorphic and False otherwise. This problem can be solved using various techniques, such as creating a dictionary to store mappings between characters or using two separate dictionaries to check for both one-to-one and onto mappings.

Method 1 - Using Dictionary to Store Mappings Between Characters

The program can be summarized as follows:

  • Checks whether the lengths of both strings are the same or not. If not, return False. Else, follow the following steps.
  • Create an empty dictionary to store mappings between characters.
  • Using the for loop, iterate over each character of both strings using indices.
    • Check if the str1[i] is present in the dictionary or not as a key.
    • If not,
      • Check whether str2[i] has been mapped to a different character or not
      • If yes, return False
      • Else, Add a new mapping between str1[i] and str2[i]
    • If yes, check whether the mapping between str1[i] and str2[i] is consistent or not.
      • If not, return False

If we made it through the loop without finding any inconsistency, return True, indicating that the strings are isomorphic.

Output:

The two strings are isomorphic.

Explanation:

In the above program, the isIsomorphic uses a dictionary called char_map to store mappings between characters in str1 and str2. and first checks if str1 and str2 have the same length since two strings can only be isomorphic if they have the same number of characters. If the two strings have different lengths, the function returns False.

Then the function loops through each character in str1 and checks if the current character has been mapped to a character in str2 yet. If the current character has not been mapped, the function adds a new mapping between the characters in char_map. If the current character has already been mapped, the function checks if the mapping is consistent with the current character in str2. If the mapping is not consistent, the function returns False.

If the function makes it through the loop without finding any inconsistencies, it returns True, indicating that the two strings are isomorphic.

Method 2 - Using Two Separate Dictionary to Check for One-to-One and Onto Mappings

The program can be summarized as follows:

  • Checks whether the lengths of both strings are the same or not. If not, return False. Else, follow the following steps.
  • Create Two dictionaries, say char_map1 and char_map2
  • Iterate over each character in str1 and str2 using a for loop.
  • Check if the current character str1[i] has been mapped or not.
  • if str1[i] not in char_map1:
    • Check if the str2[i] has already been mapped to a different character.
    • if str2[i] in char_map2: return False
  • Add new mappings between characters:
    • char_map1[str1[i]] = str2[i]
    • char_map2[str2[i]] = str1[i]
  • If the current character str1[i] has been already mapped
    • Check if the mapping is consistent with the current character in str2
    • If char_map1[str1[i]] != str2[i] or char_map2[str2[i]] != str1[i]: return False
  • If we are able to make it through the loop without finding any inconsistencies, return True, indicating that the strings are isomorphic.

Output:

The two strings are isomorphic.

Explanation:

The isIsomorphic function first checks if str1 and str2 have the same length since two strings can only be isomorphic if they have the same number of characters. If the two strings have different lengths, the function returns False.

The function uses two dictionaries called char_map1 and char_map2 to store mappings between characters in str1 and str2.

The function then uses a for-loop to iterate over each character in str1, and checks if the current character has been mapped to a character in str2 yet.

If the current character in str1 has not been mapped yet, the function checks if the current character in str2 has already been mapped to a different character. If the current character in str2 has already been mapped to a different character, the function returns False.

Otherwise, the function adds new mappings between the characters in char_map1 and char_map2.

If the current character in str1 has already been mapped, the function checks if the mappings in char_map1 and char_map2 are consistent with the current character in str2. If the mappings are not consistent, the function returns False.

If the function makes it through the loop without finding any inconsistencies, it returns True, indicating that the two strings are isomorphic.

Method 3 - Using zip() function to iterate over each character

Output:

The two strings are isomorphic.

Explanation:

The isIsomorphic() function first checks if the two strings have the same length. If they do not have the same length, the function returns False since isomorphic strings must have the same length.

Then it creates an empty dictionary called char_map to store mappings between characters in str1 and str2.

It then loops through each character in both strings simultaneously using the zip() function, which returns an iterator that aggregates elements from each of the input strings.

For each character in str1, the function checks if the character has already been mapped by checking if the character exists as a key in the char_map dictionary. If the character is already in char_map, the function checks if the mapping is consistent with the current character in str2. If the mapping is inconsistent, the function returns False since the two strings are not isomorphic.

If the character is not already in char_map, the function checks if the current character in str2 has already been mapped to a different character by checking if the character exists as a value in the char_map dictionary. If the character is already in char_map.values(), the function returns False since the two strings are not isomorphic.

Else, it adds a new mapping between the characters and moves to the next iteration. At the end of the program, if the function makes it through the loop without finding any inconsistencies, it returns True, indicating that the strings are isomorphic.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA