w3resource

Python: Check if two given strings are isomorphic to each other or not

Python Basic - 1: Exercise-69 with Solution

In abstract algebra, a group isomorphism is a function between two groups that sets up a one-to-one correspondence between the elements of the groups in a way that respects the given group operations. If there exists an isomorphism between two groups, then the groups are called isomorphic.
Two strings are isomorphic if the characters in string A can be replaced to get string B
Given "foo", "bar", return false.
Given "paper", "title", return true.
Write a Python program to check if two given strings are isomorphic to each other or not.

Sample Solution:

Python Code:

# Function to check if two strings are isomorphic
def isIsomorphic(str1, str2):          
    dict_str1 = {}  # Dictionary to store mapping for characters in str1
    dict_str2 = {}  # Dictionary to store mapping for characters in str2
    
    # Enumerate through characters in str1 and store their positions in the dictionary
    for i, value in enumerate(str1):
        dict_str1[value] = dict_str1.get(value, []) + [i]
            
    # Enumerate through characters in str2 and store their positions in the dictionary
    for j, value in enumerate(str2):
        dict_str2[value] = dict_str2.get(value, []) + [j]
    
    # Check if the values (positions) in the dictionaries are the same after sorting
    if sorted(dict_str1.values()) == sorted(dict_str2.values()):
        return True
    else:
        return False

# Test cases
print(isIsomorphic("foo", "bar"))
print(isIsomorphic("bar", "foo"))
print(isIsomorphic("paper", "title"))
print(isIsomorphic("title", "paper"))
print(isIsomorphic("apple", "orange"))
print(isIsomorphic("aa", "ab"))
print(isIsomorphic("ab", "aa"))

Sample Output:

False
False
True
True
False
False
False

Explanation:

Here is a breakdown of the above Python code:

  • Define a function named "isIsomorphic()" that takes two strings (str1 and str2) as input.
  • Create two dictionaries (dict_str1 and dict_str2) to store the mapping of characters to their positions in each string.
  • Use "enumerate" to iterate through characters in 'str1' and 'str2', and update the dictionaries accordingly.
  • Check if the values (positions) in the dictionaries are the same after sorting.
  • Return 'True' if the strings are isomorphic, and 'False' otherwise.
  • Test the function with various input strings and print the results.

Visual Presentation:

Python: Count the number of prime numbers less than a given non-negative number
Python: Count the number of prime numbers less than a given non-negative number

Flowchart:

Flowchart: Python - Count the number of prime numbers less than a given non-negative number

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to count the number of prime numbers less than a given non-negative number.
Next: Write a Python program to find the longest common prefix string amongst an given array of strings. Return false If there is no common prefix.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.