Skip to content

Instantly share code, notes, and snippets.

@wperron
Created October 16, 2023 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wperron/1f7918c24a689698e5d3e74101e394f5 to your computer and use it in GitHub Desktop.
Save wperron/1f7918c24a689698e5d3e74101e394f5 to your computer and use it in GitHub Desktop.
Isomorphic Strings
// Given two strings s and t, determine if they are isomorphic.
// Two strings are isomorphic if there is a one-to-one mapping
// possible for every character of the first string to every
// character of the second string.
function isIsomorphic(a, b) {
if (a.length !== b.length) return false;
let mapping = {};
for (let i = 0; i < a.length; i++) {
if (mapping[a[i]] === undefined) {
mapping[a[i]] = b[i];
continue;
}
if (mapping[a[i]] !== b[i]) return false;
}
return true;
}
if (import.meta.main) {
console.log(isIsomorphic("abb", "bcc"));
console.log(isIsomorphic("william", "foobaar"));
console.log(isIsomorphic("foobar", "baz"));
console.log(isIsomorphic("will", "1233"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment