-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest_Palindrome.py
More file actions
36 lines (23 loc) · 813 Bytes
/
Longest_Palindrome.py
File metadata and controls
36 lines (23 loc) · 813 Bytes
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
34
35
36
Given a string which consists of lowercase or uppercase letters, find the length of the
longest palindromes that can be built with those letters.
This is case sensitive, for example "Aa" is not considered a palindrome here.
Note:
Assume the length of given string will not exceed 1,010.
Example:
Input:
"abccccdd"
Output:
7
Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
class Solution:
def longestPalindrome(self, s: str) -> int:
final_length = 0
count = 0
mappings = Counter(s)
for key in mappings:
final_length += mappings[key]//2 * 2
count += mappings[key]%2
if count > 0:
return final_length + 1
return final_length