[LeetCode] Top Interview 150 Problems Solving # 205 Isomorphic Strings

Ramhee YeonRamhee Yeon
3 min read

Understanding the Problem

There are strings as s and t. The length of s and length of t are equal as len(s) == len(t). This problem requires deciding whether s and t are isomorphic, which means they have the same structure, or pattern.

As an example, when there are strings s = “add” and t = “egg”, ”a” could be substituted by ”e”, and ”d” could be substituted by ”g”. If the pattern ”a””e” is not met properly, it is not isomorphic anymore, thus return False.

# example 1
Input: s = "add", t = "egg"
Output: True

# example 2
Input: s = "add", t = "ege"
Output: False

Approach

This problem was somewhat similar to one of those problems that I solved already. First I would declare d dictionary and one by one add up to the dictionary if the key and value do not exist in it, otherwise return False.

Solution

I would go with 1 for loop and another for loop for value checking in the dictionary. It is to use vals in if conditions.

  • 1st if checks whether key and value are already positioned in d. If they all do not exist in d, simply make a key-value in d.

  • 2nd if checks if key does not exist in d, but value exists in it.

  • 3rd also checks if key exists in d but if the value is not equal to what is already in d.

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        d = {}

        for i, j in zip(s, t):
            vals = [v for v in d.values()]

            if i not in d and j not in vals:
                d[i] = j
            elif i not in d and j in vals:
                return False
            elif i in d and d[i] != j:
                return False

        return True

The solution above seemed pretty okay but using two for loops was a little disturbing. It took 15ms to run the entire test case. So I have removed vals as for loop, instead I used append() to vals so it will not iterate for every loop.

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        # saved runtime by 11ms!
        d = {}
        vals = []

        for i, j in zip(s, t):
            if i not in d and j not in vals:
                d[i] = j
                vals.append(j)
            elif i not in d and j in vals:
                return False
            elif i in d and d[i] != j:
                return False

        return True

This solution passed with 4ms, which is faster by 11ms than the previous one.

Reflection

Hashmap, or dictionary, problem is about checking keys, values, insert and removing and others, I think. But considering the time complex is also important. I would better practice reducing time complex to avoid unnecessary memory usage.

0
Subscribe to my newsletter

Read articles from Ramhee Yeon directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ramhee Yeon
Ramhee Yeon

South Korean, master's degree of AI. Programmer for LLM, app and web development. I am seeking opportunities to go to the USA and Canada, and Europe. I used to live in Frankfurt, Moscow, Pretoria, Baguio. Where to next?