My AC C++ code using unordered_map


  • -1
    F
    class Solution {
    

    public:
    bool isIsomorphic(string s, string t) {
    if(s.length() != t.length()){
    return false;
    }
    unordered_map<char,char> smp;
    unordered_map<char,char> tmp;
    int len = t.length();
    for(int i=0;i<len;i++){
    if(smp.find(s[i]) == smp.end() && tmp.find(t[i])==tmp.end()){
    smp[s[i]] = t[i];
    tmp[t[i]] = s[i];
    }else{
    if(smp.find(s[i]) == smp.end() && tmp[t[i]]!=s[i]){
    return false;
    }else if(tmp.find(t[i])==tmp.end() && smp[s[i]]!=t[i]){
    return false;
    }else if(smp[s[i]] != t[i] && tmp[t[i]] != s[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.