Skip to content

Instantly share code, notes, and snippets.

@av1v3k
Created April 28, 2023 06:20
Show Gist options
  • Save av1v3k/b9a1019edc027a7755b0a29724665030 to your computer and use it in GitHub Desktop.
Save av1v3k/b9a1019edc027a7755b0a29724665030 to your computer and use it in GitHub Desktop.
compare 2 string are isomorphic #code #isomorphic
// Globals
// DOM elements
const stringOne = document.querySelector("#string-one");
const stringTwo = document.querySelector("#string-two");
const message = document.querySelector("#result");
// Initialize application
function isomorphic() {
// get inputed strings
string1 = stringOne.value;
string2 = stringTwo.value;
// remove white spaces from inputed strings
strOne = string1.trim();
strTwo = string2.trim();
// get length of trimmed inputed strings
lenS1 = strOne.length;
lenS2 = strTwo.length;
if (lenS1 !== 0 && lenS2 === 0) {
message.style.color = "red";
message.innerHTML = "Please Input String Two"
return false;
} else if (lenS1 === 0 && lenS2 !== 0) {
message.style.color = "red";
message.innerHTML = "Please Input String One"
return false;
} else if (!strOne || !strTwo) {
message.style.color = "red";
message.innerHTML = "Please Input String One and String Two"
return false;
}
// check for equal length of strings
if (lenS1 != lenS2) {
message.style.color = "red";
message.innerHTML = "Please Input Strings of same length"
return false;
}
// check for one to one mapping
let chMap = {};
for (let i = 0; i < lenS1; i++) {
if (!chMap[strOne[i]]) {
chMap[strOne[i]] = strTwo[i];
} else if (chMap[strOne[i]] !== strTwo[i]) {
message.style.color = "red";
message.innerHTML = "False"
return false;
}
}
message.style.color = "green";
message.innerHTML = "True"
return true;
}
// Ref: https://codepen.io/soyedotun/pen/MBLdrj?editors=0010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment