C++ solution 11ms, using two extra vectors to record the mapping


  • 0
    Y
    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            if(s.length()!= t.length()) return false;
            char str[128];
            vector<bool> status(128, true);
            for(int i=0; i<128;i++){
                str[i] = NULL;
            }
            int len = s.length();
            for(int i = 0; i<len; i++){
                int index1 = s[i] -NULL;
                int index2 = t[i] -NULL;
                if(str[index1]==NULL&&status[index2]){
                    str[index1]=t[i];
                    status[index2]=false;
                }
                else if(str[index1]!=0){
                    if(str[index1]!=t[i]) return false;
                }
                else
                    return false;
            }
            return true;
        }
    };
    

    In the above code, I create two 128 length vectors, which contain all the characters. One recording the forward mapping relationship, the other recording whether a character has been mapped to.


Log in to reply
 

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