Leetcode—205.同构字符串【简单】

2023每日刷题(五十)

Leetcode—205.同构字符串

Leetcode—205.同构字符串【简单】_第1张图片

算法思想

参考自k神思路
Leetcode—205.同构字符串【简单】_第2张图片

实现代码

class Solution {
public:
    unordered_map<char, char> s2t, t2s;
    bool isIsomorphic(string s, string t) {
        int n = s.size();
        for(int i = 0; i < n; i++) {
            char x = s[i], y = t[i];
            if(s2t.find(x) != s2t.end() && s2t[x] != y || t2s.find(y) != t2s.end() && t2s[y] != x) {
                return false;
            }
            s2t[x] = y;
            t2s[y] = x;
        }
        return true;
    }
};

运行结果

Leetcode—205.同构字符串【简单】_第3张图片
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

你可能感兴趣的:(LeetCode刷题,leetcode,算法,职场和发展,c++,经验分享,哈希表)