Share my C++ solution,let two characters map to the same number then check


  • 0
    V
    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            int len = s.size(), i = 0;
            int smap[256] = {0};
            int tmap[256] = {0};
            
            for (i = 0; i < len; ++i)
            {
                //let s[i] and t[i] map to the same number i+1
                if (smap[s[i]] == 0)
                    smap[s[i]] = i + 1;
                    
                if (tmap[t[i]] == 0)
                    tmap[t[i]] = i + 1;
                
                if (smap[s[i]] != tmap[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.