c++ using two hash maps (O(n))


  • 0
    bool isIsomorphic(string s, string t) {
        map<char, char> m1, m2;
        for (int i = 0; i < s.size(); ++i) {
            if ((m1[s[i]] && m1[s[i]] != t[i]) || (m2[t[i]] && m2[t[i]] != s[i])) return false;
            m1[s[i]] = t[i];
            m2[t[i]] = s[i];
        }
        return true;
    }
    

Log in to reply
 

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