Created
November 14, 2015 15:03
-
-
Save NKjoep/8c1163e407c63ffcdc07 to your computer and use it in GitHub Desktop.
Javascript Palindromes of Anagrams
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
/** | |
* Returns `true` if any of the given string contains a palindrome. | |
* @param {String} s the string to test | |
*/ | |
function PalindromeFinder(s) { | |
//build an array of characters from the string `s` | |
var chars = s.split(''); | |
//store how many of the same char within the string (so the array) | |
var charsOccurences = {}; | |
chars.forEach(function(c) { | |
if (charsOccurences[c] === undefined) { //create if new | |
charsOccurences[c] = 1; | |
} else { //increment | |
charsOccurences[c] = charsOccurences[c] + 1; | |
} | |
}); | |
var evens = 0; //count the evens | |
var odds = 0; //count the odds | |
for (var char in charsOccurences) { | |
var occurrs = charsOccurences[char]; | |
if (occurrs % 2 === 0) { //count evens... | |
evens = evens + 1; | |
} else { //count odds... | |
odds = odds + 1; | |
} | |
} | |
//the result: | |
// if the string length is even then `no odds chars can be present` | |
// if the string length is odd, then `JUST only one odd char can be found`. | |
var result = false; | |
if (s.length % 2 === 0) { | |
if (odds === 0) { //no odds here please... | |
result = true; | |
} | |
} else { | |
if (odds === 1) { //here we want only one odd | |
result = true; | |
} | |
} | |
return result; | |
} |
Author
NKjoep
commented
Jul 2, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment