# Function to create palindrome of
# the given string
def createPalindrome(str):
# Hashmap for counting frequency
# of characters
map = {}
# Loop for traversing on input string
for i in range(len(str)):
if ord(str[i]) in map:
map[ord(str[i])] += 1
else:
map[ord(str[i])] = 1
first = ' '
second = ' '
X = 0
Y = 0
# Counter variable
counter = 1
# Map for traversing on map
for key in map:
# Initializing first character
# and its frequency
if counter == 1:
first = chr(key)
X = map[key]
counter += 1
# Initializing second character
# and its frequency
else:
second = chr(key)
Y = map[key]
# Checking for the conditions in
# which two stringsare not possible
if ((X == 1 or Y == 1)
or (X % 2 == 1) and (Y % 2 == 1)):
# Printing output as Not
# Possible if conditions met
print("Not Possible")
# Rest of the cases except in which
# strings are not possible
# If both X and Y are Even
elif (X % 2 == 0 and Y % 2 == 0):
# Printing arrangements as below
# aabbaa
# baaaab
for i in range(1, int(X / 2) + 1):
print(first,end="")
for i in range(1, Y + 1):
print(second,end="")
for i in range(1, int(X / 2) + 1):
print(first,end="")
print(" ",end="")
for i in range(1, int(Y / 2) + 1):
print(second,end="")
for i in range(1, X + 1):
print(first,end="")
for i in range(1, int(Y / 2) + 1):
print(second,end="")
# If either X or Y is odd
elif (X % 2 != 0 or Y % 2 != 0):
if (X % 2 == 0):
for i in range(1, int(X / 2) + 1):
print(first,end="")
for i in range(1, Y + 1):
print(second,end="")
for i in range(1, int(X / 2) + 1):
print(first,end="")
print(" ",end="")
else:
for i in range(1, int(Y / 2) + 1):
print(second,end="")
for i in range(1, X + 1):
print(first,end="")
for i in range(1, int(Y / 2) + 1):
print(second,end="")
print(" ",end="")
if (X % 2 == 0):
for i in range(1, int(Y / 2) + 1):
print(second,end="")
for i in range(1, int(X / 2) + 1):
print(first,end="")
print(second,end="")
for i in range(1, int(X / 2) + 1):
print(first,end="")
#for i in range(1, int(Y / 2) + 1):
#print(second,end="");
else:
for i in range(1, int(X / 2) + 1):
print(first,end="")
for i in range(1, int(Y / 2) + 1):
print(second,end="")
print(first,end="")
for i in range(1, int(Y / 2) + 1):
print(second,end="")
#for i in range(1, int(X / 2) + 1):
#print(first)
print("")
i = 0
testCases = 3
arr = ["baaaaaab", "aaabbbb", "aaaaaab"]
# For the first string the output order may be
# different because of the way values are sorted in
# the dictionary
while (i < testCases):
str = arr[i];
print("The original String is: ",str)
print("Palindrome String: ",end="")
# Function call
createPalindrome(str);
i+=1
# This code is contributed by akashish__