C++ 15ms solution sharing


  • 1
    S
    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            int i, h1[500], h2[500];
            memset(h1, 0, 500 * sizeof(int));
            memset(h2, 0, 500 * sizeof(int));
            for (i = 0; i < s.size(); ++i)
            {
                if (h1[s[i]] != h2[t[i]]) return false;
                h1[s[i]] = i + 1; h2[t[i]] = i + 1;
            }
            return true;
        }
    };
    

    Record & compare the last occurrence of the character in a hashtable. Using 'i + 1' to avoid the conflict of zero occurrence and appear at index 0.


Log in to reply
 

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