Anagrams

Instructions:

  • Check to see if two provided strings are anagrams of eachother. One string is an anagram of another if it uses the same characters in the same quantity. Only consider characters, not spaces or punctuation. Consider capital letters to be the same as lower case

Examples:

  • anagrams('rail safety', 'fairy tales') --> True
  • anagrams('RAIL! SAFETY!', 'fairy tales') --> True
  • anagrams('Hi there', 'Bye there') --> False

Repl.it:

Code:

// First (🥥)
function anagrams(stringA, stringB) {
const aCharMap = buildCharMap(stringA);
const bCharMap = buildCharMap(stringB);
if (Object.keys(aCharMap).length !== Object.keys(bCharMap).length) {
// We don't have an anagram
return false;
}
for (let char in aCharMap) {
if (aCharMap[char] !== bCharMap[char]) {
// We don't have an anagram
return false;
}
}
// We got it
return true;
}
function buildCharMap(str) {
const charMap = {};
for (let char of str.replace(/[^\w]/g, '').toLowerCase()) {
charMap[char] = charMap[char] + 1 || 1;
}
return charMap;
}
// Second (🥝)
function cleanString(str) {
return str
.replace(/[^\w]/g, '')
.toLowerCase()
.split('')
.sort()
.join('');
}
function anagrams(stringA, stringB) {
return cleanString(stringA) === cleanString(stringB);
}