C++ 8ms - simple solution


  • 11
    R
    class Solution {
    public:
        bool isIsomorphic(string s, string t) 
        {
            const size_t n = s.size();
            if ( n != t.size())
                return false;
            
            unsigned char forward_map[256] = {}, reverse_map[256] = {};
            
            for ( int i=0; i < n; ++i)
            {
                unsigned char c1 = s[i];
                unsigned char c2 = t[i];
                
                if ( forward_map[c1] && forward_map[c1] != c2)
                    return false;
                
                if ( reverse_map[c2] && reverse_map[c2] != c1)
                    return false;
                
                forward_map[c1] = c2;
                reverse_map[c2] = c1;
            }
            
            return true;
        }
    };

Log in to reply
 

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