Skip to content

Instantly share code, notes, and snippets.

@heat
Created March 1, 2019 18:26
Show Gist options
  • Save heat/21f00a596bd19026903ad88beb16fd5e to your computer and use it in GitHub Desktop.
Save heat/21f00a596bd19026903ad88beb16fd5e to your computer and use it in GitHub Desktop.
//Hit compile and run to see a sample output.
//Read values from stdin, do NOT hard code input.
// process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
//only to read all chunk data
process.stdin.on("data", function (chunk) {
input += chunk;
});
process.stdin.on("end", function () {
let [ arr1, arr2 ] = input.split('\n');
let calculated = isAnagrams(arr1, arr2);
console.log(calculated);
});
/**
*
* @param {string} arr1
* @param {string} arr2
*/
function isAnagrams(arr1, arr2) {
let sortA = arr1.split('').sort((a, b) => a < b);
let sortB = arr2.split('').sort((a, b) => a < b);
//diff size
console.log(sortA);
console.log(sortB);
if (sortA.length !== sortB.length) {
return false;
}
for(let i = 0; i < sortA.length; i++) {
if (sortA[i] !== sortB[i]) {
console.log(`${sortA[i]} !== ${sortB[i]}`);
return false;
}
}
return true;
}
console.log(isAnagrams('ONEZINO', 'ONENOZI'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment