
Leetcode Problem 205: Isomorphic Strings
Two strings s and t are isomorphic if there exists a consistent one-to-one character mapping from s to t — every occurrence of a character in s maps to the same character in t, and no two distinct characters in s map to the same character in t (though a character may map to itself).
Given two strings s and t of the same length, determine whether they are isomorphic.
Constraints:
function isIsomorphic(s: string, t: string): boolean {
let letterMappingS = new Map<string, string>()
let letterMappingT = new Map<string, string>()
for (let i = 0; i < s.length; i++) {
if (letterMappingS.has(s.charAt(i)) && letterMappingS.get(s.charAt(i)) !== t.charAt(i)) {
return false
}
if (letterMappingT.has(t.charAt(i)) && letterMappingT.get(t.charAt(i)) !== s.charAt(i)) {
return false
}
letterMappingS.set(s.charAt(i), t.charAt(i))
letterMappingT.set(t.charAt(i), s.charAt(i))
}
return true
}
console.log(isIsomorphic('egg', 'add'))