fork download
  1. def smallest_palindrome(s):
  2. # Count the frequency of each character
  3. freq = {}
  4. for char in s:
  5. freq[char] = freq.get(char, 0) + 1
  6.  
  7. # Identify characters with odd frequency
  8. odd_freq_chars = [char for char, count in freq.items() if count % 2 != 0]
  9.  
  10. # Construct the palindrome
  11. palindrome = ''
  12. for char, count in freq.items():
  13. if count % 2 == 0:
  14. palindrome += char * (count // 2)
  15.  
  16. # Sort odd frequency characters
  17. odd_freq_chars.sort()
  18.  
  19. # Insert characters with odd frequency at the beginning and end of the palindrome
  20. for char in odd_freq_chars:
  21. palindrome = char * (freq[char] // 2) + palindrome + char * (freq[char] // 2)
  22.  
  23. return palindrome
  24.  
  25. # Input
  26. s = input()
  27.  
  28. # Output
  29. print(smallest_palindrome(s))
  30.  
Success #stdin #stdout 0.03s 9652KB
stdin
fhaigh
stdout
h