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.
Example 1:
Input: s = "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
Example 2:
Input: s = "a" Output: 1 Explanation: The longest palindrome that can be built is "a", whose length is 1.
Constraints:
1 <= s.length <= 2000s consists of lowercase and/or uppercase English letters only.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
When trying to find the longest palindrome that can be built from a collection of letters, the brute force approach is like testing every possible combination of letters to see which ones spell the same thing forwards and backwards. We exhaustively explore all ways to arrange the available letters and measure how long each successful symmetrical word turns out to be.
Here's how the algorithm would work step-by-step:
import itertools
def longest_palindrome_brute_force(input_string):
maximum_length = 0
# Generate all possible lengths and letter arrangements to find any hidden palindromes
for current_length in range(1, len(input_string) + 1):
for current_perm in itertools.permutations(input_string, current_length):
# Convert the tuple of characters back into a string for palindrome testing
candidate_string = ''.join(current_perm)
# Verify symmetry to ensure the candidate actually reads the same backwards
if candidate_string == candidate_string[::-1]:
maximum_length = max(maximum_length, len(candidate_string))
return maximum_lengthTo find the longest possible palindrome you can build, the optimal approach focuses on counting how many times each letter appears. Since a palindrome is symmetrical, you can use pairs of matching letters on both sides and optionally place one leftover letter right in the middle.
Here's how the algorithm would work step-by-step:
from collections import Counter
def longest_palindrome(input_string):
# We count frequencies to determine how many matching character pairs we can form.
letter_frequency_map = Counter(input_string)
palindrome_length = 0
has_odd_frequency = False
for frequency_count in letter_frequency_map.values():
# Even counts contribute fully to pairs, while odd counts contribute all-but-one.
palindrome_length += frequency_count // 2 * 2
if frequency_count % 2 == 1:
has_odd_frequency = True
# A single unmatched character can be placed in the center of the palindrome.
if has_odd_frequency:
palindrome_length += 1
return palindrome_length| Case | How to Handle |
|---|---|
| Empty string input | Return zero immediately since no palindrome can be formed from zero characters. |
| Single character string | Return one because a single character is technically a valid palindrome of length one. |
| String with all unique characters | Return one since only a single character can be placed in the middle as an odd-frequency element. |
| String with all identical characters | Return the exact length of the string because all characters can be paired up symmetrically. |
| Mixed case letters | Treat uppercase and lowercase letters as distinct characters by using their exact ASCII or Unicode values in our frequency count. |
| All character counts are even | Sum all frequencies entirely without adding one since no middle character is needed. |
| Maximum constraint string length | Process the string in linear time using a hash map or frequency array to scale efficiently without exceeding memory or time limits. |