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;
}
相关推荐
Want5951 小时前
C/C++圣诞树①
c语言·开发语言·c++
l1t3 小时前
轻量级XML读写库Mini-XML的编译和使用
xml·c语言·解析器
小莞尔4 小时前
【51单片机】【protues仿真】基于51单片机停车场的车位管理系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
xianyinsuifeng5 小时前
Oracle 10g → Oracle 19c 升级后问题解决方案(Pro*C 项目)
c语言·数据库·oracle
学c语言的枫子5 小时前
数据结构——双向链表
c语言·数据结构·链表
3壹7 小时前
数据结构精讲:栈与队列实战指南
c语言·开发语言·数据结构·c++·算法
etcix8 小时前
dmenux.c: integrate dmenu project as one file
c语言·前端·算法
共享家95278 小时前
优先搜索(DFS)实战
算法·leetcode·深度优先
曙曙学编程9 小时前
stm32——独立看门狗,RTC
c语言·c++·stm32·单片机·嵌入式硬件
flashlight_hi10 小时前
LeetCode 分类刷题:2563. 统计公平数对的数目
python·算法·leetcode