[leetcode]Isomorphic Strings

这个题用一个hash存一下两个字符串的对应关系就好了。我开始以为只有小写字母,其实是所有ASCII字符。开始用一个hash存的,后来因为一一对应需要用两个hash存。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        if (s.length() != t.length()) return false;
        char hash_s[256];
        char hash_t[256];
        memset(hash_s, NULL, 256);
        memset(hash_t, NULL, 256);
        for (int i = 0; i < s.length(); i++) {
            if (NULL == hash_s[s[i]] && NULL == hash_t[t[i]]) {
                hash_s[s[i]] = t[i];
                hash_t[t[i]] = s[i];
            }
            else {
               if (hash_s[s[i]] != t[i] || hash_t[t[i]] != s[i]) return false;
            }
        }
        return true;
    }
};
Licensed under CC BY-NC-SA 4.0