10 lines C++


  • 0
    bool isIsomorphic(string s, string t) {
            unordered_map<char,int>ms(128);
            unordered_map<char,int>mt(128);
            unordered_map<char,char>m(128);
            for(int i=0;i<s.size();i++){
                ms[s[i]]++;
                mt[t[i]]++;
                if(!m[s[i]]) m[s[i]]=t[i];
                if(ms[s[i]]!=mt[t[i]]||m[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.