Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

How to check if two strings are isomorphic in Python

2 Answers

0 votes
# Two strings are isomorphic if characters in one can be replaced to get the other, 
# while preserving the order.

def are_isomorphic(s: str, t: str) -> bool:
    if len(s) != len(t):
        return False

    map_st = {}
    map_ts = {}

    for c1, c2 in zip(s, t):
        # Check mapping from s → t
        if c1 in map_st:
            if map_st[c1] != c2:
                return False
        else:
            map_st[c1] = c2

        # Check mapping from t → s
        if c2 in map_ts:
            if map_ts[c2] != c1:
                return False
        else:
            map_ts[c2] = c1

    return True


print(are_isomorphic("egg", "add"))     
print(are_isomorphic("foo", "bar"))     
print(are_isomorphic("paper", "title")) 



'''
run:

True
False
True

'''

 



answered Dec 19, 2025 by avibootz
edited Dec 19, 2025 by avibootz
0 votes
def are_isomorphic(s: str, t: str) -> bool:
    return [s.index(c) for c in s] == [t.index(c) for c in t]

print(are_isomorphic("egg", "add"))     
print(are_isomorphic("foo", "bar"))     
print(are_isomorphic("paper", "title")) 



'''
run:

True
False
True

'''

 



answered Dec 19, 2025 by avibootz
...