3ms O(n) C solution


  • 0
    Y

    The idea is to use two arrays to store the mappings between corresponding characters of s and t.

    bool isIsomorphic(char* s, char* t) {
        char map[256], rmap[256];
    
        memset(map, 0, sizeof map);
        memset(rmap, 0, sizeof rmap);
        for ( ; *s; ++s, ++t)
            if (!map[*s]) {
                if (rmap[*t]) // another character already maps to *t
                    return false;
            
                map[*s] = *t;
                rmap[*t] = *s;
            } else if (map[*s] != *t)
                return false;
            
        return true;
    }

Log in to reply
 

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