Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example "Aa" is not considered a palindrome here.

Note:
Assume the length of given string will not exceed 1,010.


Example: 

Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.

Difficulty: EASY
Tags: String algorithms

Data structures

Greedy algorithms
Skill types: Data structures

Greedy algorithms

Starter code:
class Solution:
    def longestPalindrome(self, s: str) -> int:
        

Solutions:
class Solution:

	def longestPalindrome(self, s):
		re = 0
		set_s = set(s)
		flag = False
		for x in set_s:
			if s.count(x) % 2 == 0:
				re += s.count(x)
			elif s.count(x) >= 3:
				re += s.count(x) - 1
				flag = True
			elif s.count(x) == 1:
				flag = True
		if flag == True:
			re += 1
		return re


class Solution:

	def longestPalindrome(self, s):
		dict = {}
		for w in s:
			if w not in dict:
				dict[w] = 1
			else:
				dict[w] += 1
		f = 0
		rlen = 0
		for w in dict:
			if not dict[w] % 2:
				rlen += dict[w]
			else:
				if not f:
					rlen += 1
				rlen += dict[w] - 1
				f = 1
		return rlen


class Solution:

	def longestPalindrome(self, s):
		c = {}
		(cnt, flag) = (0, 0)
		for i in s:
			c[i] = c.get(i, 0) + 1
		for i in c:
			cnt += c[i] // 2 * 2
			if c[i] % 2 != 0:
				flag = 1
		return cnt + flag


class Solution:

	def longestPalindrome(self, s):
		tmp = [False] * 58
		res = 0
		for x in s:
			t = ord(x) - 65
			if tmp[t]:
				res += 2
			tmp[t] = not tmp[t]
		if res != len(s):
			res += 1
		return res


class Solution:

	def longestPalindrome(self, s):
		l = list(s)
		result = 0
		for x in set(s):
			x_count = l.count(x)
			if x_count % 2 == 0:
				result += x_count
			else:
				result += x_count - 1
		if len(l) > result:
			result += 1
		return result


class Solution:

	def longestPalindrome(self, s):
		(base, extra, result) = ({}, 0, 0)
		for letter in s:
			if letter in base:
				base[letter] += 1
			else:
				base[letter] = 1
		for value in base.values():
			if extra == 0 and value % 2 == 1:
				extra += 1
			result += value // 2 * 2
		return result + extra


class Solution:

	def longestPalindrome(self, s):
		dic = {}
		for ss in s:
			dic[ss] = dic.get(ss, 0) + 1
		res = 0
		for (_, value) in list(dic.items()):
			res += value // 2 * 2
		if res < len(s):
			res += 1
		return res


class Solution:

	def longestPalindrome(self, s):
		dic = {}
		max_odd = 0
		for i in s:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		ans = 0
		single = 0
		for c in dic:
			if dic[c] % 2 == 0:
				ans += dic[c]
			else:
				ans += dic[c] - 1
				single = 1
		return ans + single


class Solution:

	def longestPalindrome(self, s):
		counter = {}
		longest = 0
		for char in s:
			counter[char] = counter.get(char, 0) + 1
		for (key, val) in counter.items():
			if val % 2 == 0:
				longest += val
			else:
				longest += val - 1
		if longest < len(s):
			longest += 1
		return longest


class Solution:

	def longestPalindrome(self, s):
		(count_map, has_odd, ans) = ({}, 0, 0)
		for c in s:
			try:
				count_map[c] += 1
			except KeyError:
				count_map[c] = 1
		for count in list(count_map.values()):
			if ans % 2 != 0 and count % 2 != 0:
				ans += count - 1
			else:
				ans += count
		return ans