Created
August 14, 2020 21:59
-
-
Save RP-3/6cc208e37c147b4a93f41a074692d6fb 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 {number} | |
*/ | |
var longestPalindrome = function(s) { | |
const counts = new Array(123 - 65).fill(0); | |
const A = 'A'.charCodeAt(0); | |
for(let i=0; i<s.length; i++){ // the only branching condition! | |
counts[s.charCodeAt(i) - A]++; | |
} | |
let result = 0; | |
let oddFound = 0; | |
for(let i=0; i<counts.length; i++){ // assuming a compiler would unroll this ;) | |
result += (counts[i] - (counts[i]%2)); | |
oddFound |= (counts[i] & 1); | |
} | |
return result + oddFound; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment