Longest Palindrome in a String

You are given a string s containing uppercase and lowercase English letters.

Using the characters available in s, determine the maximum possible length of a palindrome that can be formed. Each character can be used at most as many times as it appears in the original string.

Characters are case-sensitive, so uppercase and lowercase versions of the same letter are considered different characters. For example, 'A' and 'a' are different.

Return the length of the longest palindrome that can be constructed.

Example 1:

Input: s = "aaabbcccc"

Output: 9

Explanation: All 9 characters can be arranged to form a palindrome. For example, "acbcacbca" is a palindrome, so the maximum possible length is 9.

Example 2:

Input: s = "aabbcd"

Output: 5

Explanation: The pairs 'aa' and 'bb' contribute 4 characters. One of the remaining characters, such as 'c', can be placed in the center. Therefore, the longest possible palindrome has length 5.

Now Your Turn!

Pick the correct output for the given input

Input: s = "AaBbA"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 2000
  • s consists only of lowercase and uppercase English letters.
0
class Solution {
public:
int longestPalindrome(string s) {
// Your code goes here
return 0;
}
};
Test Case

Input:

S