A palindrome is a series of characters that read the same forwards as backwards such as "hannah", "racecar" and "lol".

For this Kata you need to write a function that takes a string of characters and returns the length, as an integer value, of longest alphanumeric palindrome that could be made by combining the characters in any order but using each character only once. The function should not be case sensitive.

For example if passed "Hannah" it should return 6 and if passed "aabbcc_yYx_" it should return 9 because one possible palindrome would be "abcyxycba".

Difficulty: introductory

Starter code:

def longest_palindrome(s):
	

Solutions:
from collections import Counter

def longest_palindrome(s):
    c = Counter(filter(str.isalnum, s.lower()))
    return sum(v//2*2 for v in c.values()) + any(v%2 for v in c.values())

from collections import Counter
import re

def longest_palindrome(s):
    n, odds, c = 0, 0, Counter(re.sub(r'\W+|_', '', s.lower()))
    for v in c.values():
        x,r = divmod(v,2)
        n += 2*x
        odds += r
    return n + bool(odds)

from collections import Counter

def longest_palindrome(s):
    freq = Counter(c for c in s.lower() if c.isalnum())
    even, odd = [], []
    for k, v in freq.items():
        if v % 2:    odd.append(v-1)
        else:        even.append(v)
    return sum(even) + (sum(odd) + 1 if odd else 0)

def longest_palindrome(s):
    new_s = ''.join([x for x in s.lower() if x.isalnum()])
    k = [new_s.count(x) for x in set(new_s)]

    res = 0
    for x in k:
        if x%2==0:res += x
        else:res += (x-1)
    
    return res+1 if any(x%2==1 for x in k) else res


from collections import Counter
def longest_palindrome(s):
    if s == "":
        return 0
    s = s.upper()
    x = Counter(s)
    len = 0
    flag = False
    for i in x:
        if i.isalnum():
          if x[i] % 2 == 0:
            len+=x[i]
          else:
            flag=True
            len+= (x[i]-1)
    if flag == True:
        return len+1
    return len

from collections import Counter
from string import ascii_letters, digits

def longest_palindrome(data):
    cnt = Counter([d for d in data.lower() if d in ascii_letters + digits])
    even = sum([n for n in list(cnt.values()) if n % 2 == 0 ])     
    odd = sorted( [n for n in list(cnt.values()) if n % 2 != 0] )[::-1]
    if len(odd) == 0:
        ans = even
    if len(odd) == 1:
        ans = even + odd[0]
    if len(odd) >  1:
        ans = even + odd[0] + sum([n-1 for n in odd[1:]])

    return ans

    


from collections import Counter

def longest_palindrome(string):
    possibles, has_odd = 0, False
    for char, c in Counter(string.lower()).items():
        if not char.isalnum():
            continue
        if c & 1:
            possibles += (c - 1)
            has_odd = True
        else:
            possibles += c
    return possibles + (1 if has_odd else 0)

from collections import Counter

def longest_palindrome(s):
    d, r = Counter([x for x in s.lower() if x.isalnum()]), 0
    for x in d.values():
        r += x // 2 * 2 + (x % 2 >> r % 2)
    return r

from collections import Counter

def longest_palindrome(s):
    flag = res = 0
    for v in Counter(map(str.lower, filter(str.isalnum, s))).values():
        flag |= v&1
        res += v>>1<<1 # This is just for fun, don't write that
    return res + flag

import re

def longest_palindrome(s):
    s = list(re.sub('[\W_]+', '', s.lower()))
    solution = 0
    j = 0
    letter_count = 0
    for letter in s:
        letter_count = s.count(letter)
        if letter_count % 2 == 0:
            solution += letter_count
        else:
            solution += letter_count - 1
            j = 1
        s = [i for i in s if i != letter]
    return solution + j