-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#0409 Longest Palindrome.py
33 lines (26 loc) · 1.27 KB
/
#0409 Longest Palindrome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
def longestPalindrome(self, s: str) -> int:
"""
This function calculates the length of the longest palindrome that can be formed using the characters in the string `s`.
Args:
s (str): The input string containing characters.
Returns:
int: The length of the longest palindrome that can be formed.
"""
seen = {}
# Count the frequency of each character in the string
for l in s:
seen[l] = seen.get(l, 0) + 1
odd_found = False # Flag to check if there's any character with an odd frequency
total = 0 # This will store the length of the longest possible palindrome
# Calculate the length of the palindrome
for l in seen:
if seen[l] % 2 == 0: # If the frequency of the character is even
total += seen[l] # It can be fully included in the palindrome
else: # If the frequency is odd
total += seen[l] - 1 # Include the maximum even part
odd_found = True # Mark that we have found an odd frequency
# If any odd frequency was found, we can add one more character in the middle of the palindrome
if odd_found:
total += 1
return total