Longest Palindrome - Problem
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
Letters are case sensitive, for example, "Aa" is not considered a palindrome.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abccccdd"
›
Output:
7
💡 Note:
One longest palindrome is "dccaccd". We can use: 2 d's, 4 c's, and 1 a in the center.
Example 2 — Single Character
$
Input:
s = "a"
›
Output:
1
💡 Note:
The longest palindrome is "a" itself, which has length 1.
Example 3 — All Pairs
$
Input:
s = "aabbcc"
›
Output:
6
💡 Note:
All characters have even counts, so we can use all of them: "abccba" has length 6.
Constraints
- 1 ≤ s.length ≤ 2000
- s consists of lowercase and/or uppercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code