Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created April 2, 2024 18:33
Show Gist options
  • Save tatsuyax25/a29c699b9822de36982078a8df6e0329 to your computer and use it in GitHub Desktop.
Save tatsuyax25/a29c699b9822de36982078a8df6e0329 to your computer and use it in GitHub Desktop.
var isIsomorphic = function(s, t) {
// Create two empty objects to store the mapping of characters
var mapS = {};
var mapT = {};
// Iterate over the length of the first string
for (var i = 0; i < s.length; i++) {
// If the character in s doesn't exist in mapS, add it
if (!mapS[s[i]]) {
mapS[s[i]] = t[i];
}
// If the character in t doesn't exist in mapT, add it
if (!mapT[t[i]]) {
mapT[t[i]] = s[i];
}
// If the current character in s doesn't map to the same character in t, or vice versa, return false
if (mapS[s[i]] !== t[i] || mapT[t[i]] !== s[i]) {
return false;
}
}
// If all characters map correctly, return true
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment