DEV Community

duccanhole
duccanhole

Posted on

code every day with me

--DAY 25--

Hi, I am going to make #100DaysOfCode Challenge. Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:
Problem: Isomorphic Strings
Detail: here
Idea: Use hashmap to store value of each string then compare them
My solution (javascript):

var isIsomorphic = function(s, t) {
    let map1 = new Map(), map2 = new Map();
    if (s.length != t.length) return false;
    for (let i = 0; i < s.length; i++) {
        if((map1.has(s[i])&&map1.get(s[i])!=t[i])||
           (map2.has(t[i])&&map2.get(t[i])!=s[i])
          ) return false;
        map1.set(s[i],t[i]);
        map2.set(t[i],s[i]);
    }
    return true;
};
Enter fullscreen mode Exit fullscreen mode

-->If you have better solution or any question, please comment below. I will appreciate.

Top comments (0)