Last active
March 8, 2016 04:36
-
-
Save aire-con-gas/eb3abcf76c579a686d26 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
| /** | |
| * @param {string} s | |
| * @return {boolean} | |
| */ | |
| var canPermutePalindrome = function(s) { | |
| var charTable = {}, | |
| charSet = { | |
| 'even': 0, | |
| 'odd': 0 | |
| }, | |
| inputArr = s.split(''), | |
| isPalindrome = false, | |
| k; | |
| for(var i=0, il = inputArr.length; i < il; i++){ | |
| k = inputArr[i]; | |
| if(k in charTable) { | |
| charTable[k] += 1; | |
| } else { | |
| charTable[k] = 1; | |
| } | |
| } | |
| if(Object.keys(charTable).length > 1) { | |
| for(var j in charTable){ | |
| if(charTable[j] % 2 === 0){ | |
| charSet['even'] += 1; | |
| } else { | |
| charSet['odd'] += 1; | |
| } | |
| } | |
| if(charSet['even'] > 0 && charSet['odd'] === 0){ | |
| isPalindrome = true; | |
| } else if(charSet['even'] > 0 && charSet['odd'] > 0){ | |
| if(charSet['even'] >= charSet['odd']) { | |
| isPalindrome = true; | |
| } | |
| } | |
| } else { | |
| isPalindrome = true; | |
| } | |
| return isPalindrome; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment