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;
}
相关推荐
菜鸟233号7 分钟前
力扣78 子集 java实现
java·数据结构·算法·leetcode
json{shen:"jing"}1 小时前
2-C语言的运算符和表达式
c语言·开发语言
月明长歌1 小时前
【码道初阶】【Leetcode94&144&145】二叉树的前中后序遍历(非递归版):显式调用栈的优雅实现
java·数据结构·windows·算法·leetcode·二叉树
DanyHope1 小时前
《LeetCode 49. 字母异位词分组:哈希表 + 排序 全解析》
算法·leetcode·哈希算法·散列表
iAkuya1 小时前
(leetcode) 力扣100 15轮转数组(环状替代)
数据结构·算法·leetcode
沪漂的码农1 小时前
UDS诊断物理层时间参数详解技术文章
c语言·can·uds
努力学算法的蒟蒻1 小时前
day38(12.19)——leetcode面试经典150
算法·leetcode·面试
iAkuya2 小时前
(leetcode)力扣100 17缺失的第一个正数(哈希)
算法·leetcode·哈希算法
黎雁·泠崖2 小时前
【C语言指针精讲】从内存到运算,吃透指针核心逻辑
c语言·开发语言
秦苒&2 小时前
【C语言指针四】数组指针变量、二维数组传参本质、函数指针变量、函数指针数组
c语言·开发语言·c++·c#