leetcode +判断两字符串是否同构,双向验证hash

点击打开链接
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        if(s.size() != t.size()) return false;
        map<char, char> s2t, t2s;
        for(int i=0; i<s.size(); i++){
            if(s2t.find(s[i]) == s2t.end()){
                s2t.insert({s[i], t[i]});
            }
            else{
                if(s2t[s[i]] != t[i]) return false;
            }
            if(t2s.find(t[i]) == t2s.end()){
                t2s.insert({t[i],s[i]});
            }
            else{
                if(t2s[t[i]] != s[i]) return false;
            }
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/u013554860/article/details/81052117