C++ 8 ms beats 69.99%. only one mapping vector is used


  • 0
    W
    bool isIsomorphic(string s, string t) {
            
            /*ASCII code 0: neither this character maps to any character nor it is mapped by certain character.
              ASCII code 1: this character is mapped by certain character*/
            vector<char> hashTable(128, 0);
            bool rtn = true;
            if(s.empty() | t.empty())   return true;
            
            for(int i = 0; i < s.size(); i++)
            {
                if(hashTable[s[i]] == t[i])
                {   continue;   }
                
                if((hashTable[t[i]] == 1) || (hashTable[t[i]] == t[i]))
                {
                    /*multiple keys are mapped to the same value*/
                    rtn = false;
                    break;
                }
                else if((hashTable[s[i]] != 0) && (hashTable[s[i]] != 1) && (hashTable[s[i]] != t[i]))
                {
                    /*one key is mapped to multiple value*/
                    rtn = false;
                    break;
                }
                else
                {
                    hashTable[s[i]] = t[i];
                    if(hashTable[t[i]] == 0)
                    {   hashTable[t[i]] = 1;    }
                }
            }
            return rtn;
        }
    

  • 0

    The code is wrong.

    Try

    "abd"
    "bcb"


  • 0
    W

    I tried your test case. Both my result and the expected result return true. Can you elaborate on your comment? Thanks.


  • 0

    But the answer should be false. "abd" is not the same pattern as "bcb", isn't this obvious?


  • 0

    By the way, when I run the case, the expected output of the system is "false"


Log in to reply
 

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