[RainbowSecret] C++ CODE


  • 1

    This is my first wrong solution ........ Because I lose the details that the different key can not be mapped to the same value.

    It means that the mapping is bidirectional ......

    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            int size_s=s.size(), size_t=t.size();
            if(size_s!=size_t)  return false;
            
            unordered_map<char, char> dict;
            for(int i=0; i<size_s; i++){
                if(dict.find(s[i])!=dict.end()){
                    if(dict[s[i]]!=t[i])  return false;
                }
                else{
                    dict[s[i]]=t[i];
                }
            }
            
            return true;
        }
    };
    

    To solve this problem , we need to use 2 map record both side mapping information.

        vector<int> dict_s(128, 0);
        vector<int> dict_t(128, 0);
    

    Here is the AC implementation

    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            int size_s=s.size(), size_t=t.size();
            if(size_s!=size_t)  return false;
            
            vector<int> dict_s(128, 0);
            vector<int> dict_t(128, 0);
            
            for(int i=0; i<size_s; i++){
                if(dict_s[s[i]]!=dict_t[t[i]])  return false;
                dict_s[s[i]]=i+1;
                dict_t[t[i]]=i+1;
            }
            
            return true;
        }
    };
    

    We use dict_s[s[i]]=i+1; because the state 0 is all the initial state .... We can not use it to avoid unnecessary bugs.


Log in to reply
 

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