Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created August 14, 2020 21:59
Show Gist options
  • Save RP-3/6cc208e37c147b4a93f41a074692d6fb to your computer and use it in GitHub Desktop.
Save RP-3/6cc208e37c147b4a93f41a074692d6fb to your computer and use it in GitHub Desktop.
/**
* @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