题意:给两个长度相同的字符串,如果其中一个可以通过字母的替换获得另一个字符串(且不同的字母不能替换成相同的字母),则返回true。
solution:hash表记录,s->t中字母的映射关系,同时记录t中哪些字母已经被替换过了。
class Solution {
public:
bool isIsomorphic(string s, string t) {
unordered_map<char, char> stmap;
unordered_map<char, bool> maped;
for ( int i = 0; i < s.size(); i++ ) {
if ( stmap.find(s[i]) == stmap.end() ) { // no map
if ( maped.find(t[i]) != maped.end() ) return false;
stmap[s[i]] = t[i];
maped[t[i]] = 1;
}
else if ( stmap[s[i]] != t[i] ) return false;
}
return true;
}
};
submission: