409-longest-palindrome

DevGod
Vtuber
/** * @param {string} s * @return {number} */var longestPalindrome = function(s) { let myMap = new Map(); for(let chr of s){ if(!myMap.has(chr)){ myMap.set(chr,0); } myMap.set(chr, myMap.get(chr)+1); }
let oddCount = 0; for(let val of myMap.values()){ if(val%2 == 1){ oddCount++; } }
if(oddCount == 0){return s.length;}
return s.length-(oddCount-1);};
//abccccdd
//ccdadcc
//b
//abbcccdddd//ddcbabcdd
//c
//bbaaaa
class Solution: def longestPalindrome(self, s: str) -> int: myMap = Counter(s) score = 0 hasOdd = 0 for val in myMap.values(): score += floor(val/2)*2 if(val%2 == 1): hasOdd = 1 return score+hasOdd