Problem:I need to write an algorithm that will return length of longest possible palindrome from a given string.
So if the input is aabbbccdfg Program output should be 7. //-->[cabbbac]
Can someone point it out if there are any problems with below code?
public static void GetLongestPalindromeLength()
{
var input = Console.ReadLine().ToCharArray();
var result = 0;
var countsByChar = input.GroupBy(c => c);
var oddCounts = countsByChar.Where(g => g.Count() % 2 != 0).Select(g => g.Count()).ToList();
var evenCounts = countsByChar.Where(g => g.Count() % 2 == 0).Select(g => g.Count()).ToList();
if (oddCounts.Any())
{
var max = oddCounts.Max();
result += max;
oddCounts.RemoveAt(oddCounts.FindIndex(e => e == max));
result += evenCounts.Sum();
result += oddCounts.Sum(e => e - 1);
}
else if (evenCounts.Any())
{
result += evenCounts.Sum();
}
Console.WriteLine(result);
Console.ReadKey();
}