#205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
这道题是建立两个映射,第一个从s到t, 第二个从t到s。判断是不是每个字母都符合这个映射,不符合就返回false,全都符合就返回true。
var isIsomorphic = function(s, t) {
let map = {};
let map2 = {};
for(let i=0;i<s.length;i++) {
if(map[s[i]]) {
if(map[s[i]] !== t[i]) {
return false;
}
}
else {
map[s[i]] = t[i];
}
}
for(let i=0;i<s.length;i++) {
if(map2[t[i]]) {
if(map2[t[i]] !== s[i]) {
return false;
}
}
else {
map2[t[i]] = s[i];
}
}
return true;
};