题目:
题解:
struct HashTable {
char key;
char val;
UT_hash_handle hh;
};
bool isIsomorphic(char* s, char* t) {
struct HashTable* s2t = NULL;
struct HashTable* t2s = NULL;
int len = strlen(s);
for (int i = 0; i < len; ++i) {
char x = s[i], y = t[i];
struct HashTable *tmp1, *tmp2;
HASH_FIND(hh, s2t, &x, sizeof(char), tmp1);
HASH_FIND(hh, t2s, &y, sizeof(char), tmp2);
if (tmp1 != NULL) {
if (tmp1->val != y) {
return false;
}
} else {
tmp1 = malloc(sizeof(struct HashTable));
tmp1->key = x;
tmp1->val = y;
HASH_ADD(hh, s2t, key, sizeof(char), tmp1);
}
if (tmp2 != NULL) {
if (tmp2->val != x) {
return false;
}
} else {
tmp2 = malloc(sizeof(struct HashTable));
tmp2->key = y;
tmp2->val = x;
HASH_ADD(hh, t2s, key, sizeof(char), tmp2);
}
}
return true;
}