十七
十七
Published on 2022-04-03 / 221 Visits
0
0

leetcode205. 同构字符串

leetcode205. 同构字符串

哈希

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int n=s.size(),m=t.size();
        if(n!=m)return false;
        unordered_map<char,char>ss,tt;
        for(int i=0;i<n;i++){
            char a=s[i],b=t[i];
            if(ss.count(a)&&ss[a]!=b||tt.count(b)&&tt[b]!=a)return false;
            ss[a]=b;
            tt[b]=a;
        }
        return true;
    }
};

Comment