LeetCode #205 Isomorphic Strings Solution & Explanation

LeetCode Problem

Given two strings s and t, determine if they are isomorphic.

Two strings s and t are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

See what is isomorphicIsomorphism – Wikipedia


Solution

After looking at the description of this problem, I realized that I had solved a similar problem before.

See Here ➡ LeetCode #290 Word Pattern

The only difference is that one involves characters mapping to strings, while this one involves characters mapping to characters.

In this problem, two strings have been defined with the same length, so the requirement for equal lengths can be omitted.

So, we can rewrite the function with just a slight adjustment.

C# Solution

Solution1 – Dictionary & HashSet

public class Solution {
    public bool IsIsomorphic(string s, string t) {
        Dictionary<char,char> dict =   new Dictionary<char,char>();
        HashSet<char> hSet = new HashSet<char>();

        for(int i = 0; i< s.Length;i++)
        {
            var sChar = s[i];
            var tChar = t[i];
            if (!dict.ContainsKey(sChar))
            {
                if(hSet.Contains(tChar))
                {
                    return false;
                }
                dict.Add(sChar, tChar);
                hSet.Add(tChar);
            }
            else    //check if exists
            {
                if(tChar != dict[sChar])
                {
                    return false;
                }
            }
        }

        return true;
    }
}

Java Solution

Solution1

class Solution {
    public boolean isIsomorphic(String s, String t) {
        Map<Character,Character> dict =   new HashMap<Character,Character>();
        HashSet<Character> hSet = new HashSet<Character>();

        for(int i = 0; i< s.length();i++)
        {
            var sChar = s.charAt(i);
            var tChar = t.charAt(i);
            if (!dict.containsKey(sChar))
            {
                if(hSet.contains(tChar))
                {
                    return false;
                }
                dict.put(sChar, tChar);
                hSet.add(tChar);
            }
            else    //check if exists
            {
                if(tChar != dict.get(sChar))
                {
                    return false;
                }
            }
        }

        return true;
    }
}

Python3 Solution

Solution1

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

        for i in range (len(s)):
            sChar = s[i]
            tChar = t[i]
            if (sChar not in dict):
                if(tChar in hSet):
                    return False
                
                dict[sChar] = tChar
                hSet.add(tChar)
            
            else:    #check if exists
                if(tChar != dict[sChar]):
                    return False

        return True

JavaScript Solution

Solution1

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isIsomorphic = function(s, t) {
    const dict = new Map();
    const hSet = new Set();

    for(let i = 0; i< s.length;i++)
    {
        const sChar = s.charAt(i);
        const tChar = t.charAt(i);
        if (!dict.has(sChar))
        {
            if(hSet.has(tChar))
            {
                return false;
            }
            dict.set(sChar, tChar);
            hSet.add(tChar);
        }
        else    //check if exists
        {
            if(tChar !== dict.get(sChar))
            {
                return false;
            }
        }
    }

    return true;
};

Conclusion

Nothing special, you can easily solve this problem if you have faced a similar one.

The more problems you have solved, the more confident you become =)

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks a lot

If you got any problem about the explanation, please feel free to let me know

The problem link : Isomorphic Strings – LeetCode

Some Random post

Leave a Reply

Your email address will not be published. Required fields are marked *