思路:
[s[i], t[i]]
映射的两个约束:
(1)要确保每个s[i]可以唯一的映射到t[i],但可能出现一个t[i]有多个s[i]对应;
(2)要确保每个t[i]可以唯一的映射到s[i],但可能出现一个s[i]有多个t[i]对应;
class Solution {
public:
bool isIsomorphic(string s, string t) {
if(s.length() != t.length()) return false;
map<char, char> ht;
//make sure s[i] is mapped to the unique t[i]
for(int i = 0; i < s.length(); ++i) {
if(ht.find(s[i]) == ht.end()) {
ht[s[i]] = t[i];
}else if(ht[s[i]] != t[i]){
return false;
}
}
ht.clear();
//make sure t[i] is mapped to the unique s[i]
for(int i = 0; i < t.length(); ++i) {
if(ht.find(t[i]) == ht.end()) {
ht[t[i]] = s[i];
}else if(ht[t[i] != s[i]]){
return false;
}
}
return true;
}
};