Silly C++ Solution with two unordered_map, slow but easy to understand


  • 5
    B
    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            
            if (s.size()== 0)
                return true;
            
            
            unordered_map<char, char> map1;
            unordered_map<char, char> map2;
            map1.insert(make_pair(s[0], t[0]));
            map2.insert(make_pair(t[0], s[0]));
            
            
            for (int i = 1; i< s.size(); i++){
                if (map1.find(s[i])!=map1.end()){
                    if (t[i] != map1.at(s[i])) {
                        return false;
                    }
                }
                
                if (map2.find(t[i])!=map2.end()){
                    
                    if (s[i] != map2.at(t[i])) {
                        return false;
                    }
                }
                
                else{
                    map1.insert(make_pair(s[i],t[i]));
                    map2.insert(make_pair(t[i],s[i]));
                }
                
            }
            return true;
        }
    };

  • 5
    H

    Very intuitive idea to understand really easily !
    Thanks for share.
    But little modification for bit faster than your code, don't worry, still based on your original idea

    Yours : 40 ms
    As below: 32 ms

    because ".at()" = ".find()" + "indexing access"

    {
    if (s.size()== 0)
    return true;

    unordered_map<char, char> map1 {{s[0], t[0]}};
    unordered_map<char, char> map2({{t[0], s[0]}});
    
    unordered_map<char,char>::iterator it;   <<<<<<<<<<<<< diff  1
    
    for (int i = 1; i< s.size(); i++)
    {
        it = map1.find(s[i]);                              <<<<<<<<<<<<< diff  2
        if (it!=map1.end())
            if (t[i] != it->second)                         <<<<<<<<<<<<< diff  3
                return false;
    
        it = map2.find(t[i]);                              <<<<<<<<<<<<< diff  4
        if (it!=map2.end())
        {
            if (s[i] != it->second)                        <<<<<<<<<<<<< diff  5
                return false;
        }
        else
        {
            map1.insert({{s[i],t[i]}});
            map2.insert({{t[i],s[i]}});
        }
    }
    return true;
    

    }


  • 0
    Y

    Mine is simpler , I think.

    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            unordered_map<char, char> hash, reflect;
            for(int i=0; i<s.length(); ++i) {
                if(hash.find(s[i])==end(hash) && reflect.find(t[i])==end(reflect)) {
                    hash[s[i]] = t[i];
                    reflect[t[i]] = s[i];
                }else {
                    if(hash[s[i]] != t[i])
                        return false;
                }
            }
            return true;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.