讨论/《哈希表》 - 同构字符串/
《哈希表》 - 同构字符串
共 2 个回复

Python3

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        """
        方法:哈希映射表
        """
        s_map = {}
        t_map = {}
        for i in range(len(s)):
            if (s[i] in s_map and s_map[s[i]]!=t[i]) or (t[i] in t_map and t_map[t[i]]!=s[i]):
                return False
            s_map[s[i]] = t[i]
            t_map[t[i]] = s[i]
        return True

haha,我果然是个菜鸟,两次判断才解决

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        map<char,char> mp;
        int flag = 1;
        for(int i = 0;i < s.size();i ++)
            mp[s[i]] = t[i];
        string str = s;
        for(int i = 0;i< s.size();i++)
            str[i] = mp[s[i]];
        if(str==t)
            flag = 1;
        else
            flag = 0;
        if(flag == 1)
        {
            mp.clear();
            for(int i = 0;i < s.size();i ++)
                mp[t[i]] = s[i];
            for(int i = 0;i< s.size();i++)
                t[i] = mp[t[i]];
            if(s == t)
                return true;
            else
                return false;
        }
        else
            return false;
    }
};