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:

def isIsomorphic(str1, str2):          
    dict_str1 = {}
    dict_str2 = {}
    
    for i, value in enumerate(str1):
        dict_str1[value] = dict_str1.get(value, []) + [i]
            
    for j, value in enumerate(str2):
        dict_str2[value] = dict_str2.get(value, []) + [j]
    
    if sorted(dict_str1.values()) == sorted(dict_str2.values()):
        return True
    else:
        return False

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

Pictorial 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.