题意:给两个字符串,问是否存在一种字母映射关系使得两个串相等。
题解:我们需要注意,一种字母只能映射成另外一种字母,是1对1的。因此开两个hash表存第一个串字母的映射和第二个串字母的映射。
class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char,char> m1;
map<char,char> m2;
m1.clear();
m2.clear();
int n = s.length();
for(int i = 0; i < n; i++)
{
if(m1.find(s[i]) == m1.end() && m2.find(t[i]) == m2.end()) m1[s[i]] = t[i],m2[t[i]] = s[i];
else if(m1.find(s[i]) != m1.end() && m2.find(t[i]) != m2.end() && m1[s[i]] == t[i] && m2[t[i]] == s[i]) continue;
else return false;
}
return true;
}
};