Created
April 2, 2024 18:33
-
-
Save tatsuyax25/a29c699b9822de36982078a8df6e0329 to your computer and use it in GitHub Desktop.
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
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