Check Whether Two Strings Are Isomorphic to Each Other or Not in PythonThe 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 CharactersThe program can be summarized as follows:
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 MappingsThe program can be summarized as follows:
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 characterOutput: 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. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India