1. 程式人生 > >[leetcode] Isomorphic Strings

[leetcode] Isomorphic Strings

簡單題,map記錄下即可~程式碼如下:

bool isIsomorphic(string s, string t) {
        if(s.length() != t.length())
            return false;
        unordered_map<char, char> mp1;
        unordered_map<char, char> mp2;
        for(int i = 0; i < s.length(); ++i){
            if(mp1.find(s[i]) == mp1.end() && mp2.find(t[i]) == mp2.end()){
                mp1[s[i]] = t[i];
                mp2[t[i]] = s[i];
                continue;
            }
            if(mp1.find(s[i]) == mp1.end() || mp2.find(t[i]) == mp2.end())
                return false;
            if(mp1[s[i]] != t[i] || mp2[t[i]] != s[i])
                return false;
        }
        return true;
    }