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;
}
相关推荐
薛慕昭2 小时前
嵌入式 C 语言猜大小游戏设计与实现
c语言·游戏
leoufung2 小时前
LeetCode 92 反转链表 II 全流程详解
算法·leetcode·链表
月光技术杂谈2 小时前
实战:C驱动框架嵌入Rust模块的互操作机制与完整流程
c语言·开发语言·rust·ffi·跨语言·bindgen·互操作
im_AMBER3 小时前
Leetcode 59 二分搜索
数据结构·笔记·学习·算法·leetcode
合作小小程序员小小店3 小时前
游戏开发,桌面%小游戏,贪吃蛇%demo,基于vs2022,c语言,easyX,无数据库
c语言·开发语言
leoufung3 小时前
LeetCode 61. 旋转链表(Rotate List)题解与思路详解
leetcode·链表·list
WongKyunban7 小时前
Linux中的线程是什么?
c语言
LaoZhangGong1238 小时前
以太网HTTP数据包格式分析
c语言·stm32·网络协议·http·tcp·arp
leoufung8 小时前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode
lingggggaaaa10 小时前
免杀对抗——C2远控篇&PowerShell&有无文件落地&C#参数调用&绕AMSI&ETW&去混淆特征
c语言·开发语言·笔记·学习·安全·microsoft·c#