Skip to content

Instantly share code, notes, and snippets.

@varnie
Created July 24, 2025 10:49
Show Gist options
  • Select an option

  • Save varnie/8dc7767099c00515cd7b62fe1d2ff257 to your computer and use it in GitHub Desktop.

Select an option

Save varnie/8dc7767099c00515cd7b62fe1d2ff257 to your computer and use it in GitHub Desktop.
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