Share my C++ solution,easy to understand


  • 0
    V
    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            int len = s.size(), i = 0;
            map<char, char> myMap;
            
            for (i = 0; i < len; ++i)
            {
                if (myMap.find(s[i]) == myMap.end())
                    myMap[s[i]] = t[i];
                else if (myMap[s[i]] != t[i])
                    return false;
            }
            myMap.clear();
            for (i = 0; i < len; ++i)
            {
                if (myMap.find(t[i]) == myMap.end())
                    myMap[t[i]] = s[i];
                else if (myMap[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.