/*从s->t映射可以,用hash记住每个映射,判断是否能从s->t进行映射。
反过来,再判断是否能从t->进行映射,只有能进s->t和t->的映射,才是同构的。*/
class Solution {
public:
bool isIsomorphic(string s, string t){
return s2t(s, t) && s2t(t, s);
}
bool s2t(string s, string t) {
unordered_map<char, char> replaced;
for(int i = 0; i < s.size(); ++i){
if(replaced.find(s[i]) == replaced.end()){
replaced[s[i]] = t[i];
}
else{
if(replaced[s[i]] != t[i]) return false;
}
}
return true;
}
};
LeetCode之Isomorphic Strings
最新推荐文章于 2020-10-21 23:20:36 发布