// Two strings are isomorphic if characters in one can be replaced to get the other,
// while preserving the order.
function areIsomorphic(s: string, t: string): boolean {
if (s.length !== t.length) return false;
const mapST: Map<string, string> = new Map<string, string>();
const mapTS: Map<string, string> = new Map<string, string>();
for (let i = 0; i < s.length; i++) {
const c1: string = s[i];
const c2: string = t[i];
// Check mapping from s → t
if (mapST.has(c1)) {
if (mapST.get(c1) !== c2) return false;
} else {
mapST.set(c1, c2);
}
// Check mapping from t → s
if (mapTS.has(c2)) {
if (mapTS.get(c2) !== c1) return false;
} else {
mapTS.set(c2, c1);
}
}
return true;
}
console.log(areIsomorphic("egg", "add"));
console.log(areIsomorphic("foo", "bar"));
console.log(areIsomorphic("paper", "title"));
/*
run:
true
false
true
*/