Created
September 25, 2018 19:44
-
-
Save brybrophy/0fc26a78553b9e0b63ed6de255d6a9e5 to your computer and use it in GitHub Desktop.
My Code Signal Palindrome Rearranging Solution.
This file contains 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
function palindromeRearranging(inputString) { | |
const charCounts = [...inputString].reduce((counts, char) => { | |
counts[char] = counts[char] ? counts[char] + 1 : 1; | |
return counts; | |
}, {}); | |
return Object.values(charCounts).filter(count => count % 2 !== 0).length <= 1; | |
} | |
// First get the character count for each character in the string and store it in an object. | |
// The string can only be a palindrome if either the count of ALL characters is even | |
// or if the count of ONLY ONE character is odd. | |
// Filter the object values down to just the odd character counts. | |
// If the resulting array's length is 0 or 1, the string can be a palindrome. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is cool