Simple 8ms C++ solution. O(n) time and O(1) space


  • 2
    M

    class Solution {
    public:
    bool isIsomorphic(string s, string t) {

        if(s.length() != t.length())
             return false;
        
        int sa[256] = {0};
        int ta[256] = {0};
        
        for(int i=0;i<s.length();i++)
        {
            if(sa[s[i]] == 0)
                sa[s[i]] = i+1;
            if(ta[t[i]] == 0)
                ta[t[i]] = i+1;
            
            if(sa[s[i]] != ta[t[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.