Created
July 24, 2025 10:49
-
-
Save varnie/8dc7767099c00515cd7b62fe1d2ff257 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import itertools | |
| """ | |
| The Longest Palindrome | |
| You are given a string s consisting of lowercase or uppercase letters. | |
| You need to determine the maximum length of a palindrome that can be made from the letters of this string. | |
| The letters are case-sensitive, for example, the string "Aa" is not considered a palindrome. | |
| Example 1: | |
| Input: s = "abccccdd" | |
| Output: 7 | |
| Explanation: | |
| The longest palindrome that can be constructed is "dccaccd", its length is 7. | |
| Example 2: | |
| Input: s = "a" | |
| Output: 1 | |
| Limitations: | |
| 1 <= s.length <= 2000 | |
| s consists only of lowercase or uppercase letters of the English alphabet. | |
| """ | |
| class Solution: | |
| def longest_palindrome(self, s: str) -> int: | |
| result = 0 | |
| for i in range(0, len(s)): | |
| cur_permutations = itertools.permutations(s, i+1) | |
| for word in cur_permutations: | |
| word = "".join(word) | |
| if word == word[::-1]: | |
| new_len = len(word) | |
| if not result: | |
| result = new_len | |
| else: | |
| if new_len > result: | |
| result = new_len | |
| return result | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment