All files / leetCode 0409.ts

100% Statements 10/10
100% Branches 6/6
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  3x   3x 11x 5x 6x     3x   3x 6x 3x     3x    
export default function longestPalindrome(s: string): number {
  const map = new Map<string, number>()
 
  for (const char of s) {
    if (map.has(char))
      map.set(char, (map.get(char) as number) + 1)
    else map.set(char, 1)
  }
 
  let odd = 0
 
  for (const count of map.values()) {
    if (count % 2 === 1)
      odd++
  }
 
  return odd > 1 ? s.length - odd + 1 : s.length
}