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;
}
相关推荐
烟烟Abandon7 分钟前
FFmpeg开发基础之 C 语言编译器
c语言·开发语言·ffmpeg
Zwawa1 小时前
断电后参数为什么全没了:把小车配置安全地存进 Flash
c语言·单片机·嵌入式
Tisfy2 小时前
LeetCode 3561.移除相邻字符:栈(相邻相消)
算法·leetcode·字符串·题解·栈·相邻相消
羊在吃青草3 小时前
C语言之数组
c语言·数据结构
烟烟Abandon3 小时前
FFmpeg开发基础之 C 语言中的文件操作
c语言·ffmpeg
金榜题名萱6 小时前
蓝桥杯|石子合并(区间动态规划)
c语言·数据结构·c++·算法·蓝桥杯·动态规划
Navigator_Z7 小时前
LeetCode //C - 1332. Remove Palindromic Subsequences
c语言·算法·leetcode
Code_Solitude7 小时前
c语言:期末复习(7)
c语言·开发语言·算法
金榜题名萱7 小时前
蓝桥杯 - 旗帜
c语言·数据结构·c++·算法·蓝桥杯
Nazarick8 小时前
力扣LeetCode热题(3)
java·算法·leetcode·贪心算法