Created
September 30, 2018 04:22
-
-
Save jordanhudgens/76b182400824696d1c7203582e9a96e4 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
const stringCleaner = str => { | |
const strWithoutCharacters = str.replace(/[^A-Z0-9]/gi, ""); | |
return strWithoutCharacters.toLowerCase(); | |
}; | |
const buildCharacterMap = str => { | |
const chars = str.split(""); | |
const charMap = chars.reduce((obj, char) => { | |
obj[char] ? obj[char]++ : (obj[char] = 1); | |
return obj; | |
}, {}); | |
return charMap; | |
}; | |
const isEqual = (obj1, obj2) => { | |
const obj1Keys = Object.keys(obj1); | |
const obj2Keys = Object.keys(obj2); | |
if (obj1Keys.length !== obj2Keys.length) { | |
return false; | |
} | |
for (let objKey of obj1Keys) { | |
if (obj1[objKey] !== obj2[objKey]) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
const str1 = buildCharacterMap(stringCleaner("Jim Morrison")); | |
const str2 = buildCharacterMap(stringCleaner("Mr. Mojo Risin'")); | |
isEqual(str1, str2); //? | |
const isAnagram = (str, comparisonStr) => { | |
const strObj = buildCharacterMap(stringCleaner(str)); | |
const comparisonStrObj = buildCharacterMap(stringCleaner(comparisonStr)); | |
return strObj === comparisonStrObj; | |
}; | |
isAnagram("Jim Morrison", "Mr. Mojo Risin'"); | |
isAnagram("Damon Albarn", "Dan Abnormal"); | |
isAnagram("George Bush", "He bugs Gore"); | |
isAnagram("Clint Eastwood", "Old West action"); | |
isAnagram("Ronald Reagan", "A darn long era"); | |
isAnagram("Elvis", "Lives"); | |
isAnagram("Madonna Louise Ciccone", "One cool dance musician"); | |
isAnagram("Bart", "Brat"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment