Skip to content

Instantly share code, notes, and snippets.

@brybrophy
Created September 25, 2018 19:44
Show Gist options
  • Save brybrophy/0fc26a78553b9e0b63ed6de255d6a9e5 to your computer and use it in GitHub Desktop.
Save brybrophy/0fc26a78553b9e0b63ed6de255d6a9e5 to your computer and use it in GitHub Desktop.
My Code Signal Palindrome Rearranging Solution.
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.
@IsaiUriepero
Copy link

this is cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment