C语言 | Leetcode C语言题解之第205题同构字符串

题目:

题解:

cpp 复制代码
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;
}
相关推荐
依旧风轻26 分钟前
232. 用栈实现队列 (Implement Queue using Stacks)
leetcode·ios·swift·queue·stack
灭霸112331 分钟前
力扣 用队列实现栈(Java)
算法·leetcode·职场和发展
-代号95271 小时前
【LeetCode】十、二分查找法:寻找峰值 + 二维矩阵的搜索
算法·leetcode·矩阵
小白在路上~1 小时前
51单片机嵌入式开发:STC89C52操作GPIO口LED灯
c语言·开发语言·单片机·嵌入式硬件·51单片机·dsp开发
@干吧jyb2 小时前
贪心算法练习题(7/2)
数据结构·算法·leetcode·贪心算法
Wise cas4292 小时前
C语言编程-基于单链表实现贪吃蛇游戏
c语言·开发语言·游戏
A22742 小时前
LeetCode 1327, 383, 236
java·算法·leetcode
我代码抄都抄不明白4 小时前
【无标题】蓝屏事件 139
c语言·windows·microsoft·visual studio
“αβ”8 小时前
c语言的烫烫烫烫烫??
c语言·开发语言·c++
我不会起名字呀8 小时前
在 C 语言中使用 UT_hash_handle 简化实现哈希表
c语言·链表·华为od