Simple and easy understood c++ code with O(n) time


  • 3
    X
    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            if(s.size() != t.size())
                return false;
            char words[256] = {0}, wordt[256] = {0};
            for(int i=0; i<s.size(); i++){
                if(words[s[i]] == 0)
                    words[s[i]] = t[i];
                else if(words[s[i]] != t[i])
                    return false;
                if(wordt[t[i]] == 0)
                    wordt[t[i]] = s[i];
                else if(wordt[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.