Created
October 16, 2023 14:57
-
-
Save wperron/1f7918c24a689698e5d3e74101e394f5 to your computer and use it in GitHub Desktop.
Isomorphic Strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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