CodeSignal OA | Palindrome
Anonymous User
1911

You're developing a new programming language with some unusual features for strings! Among these is a method that returns the longest palindrome that can be formed with the characters of a given string.

Given a string s, your task is to find this longest possible palindrome. You may use any number of the characters from s, and arrange them in any order (so long as it results in a palindrome).

If there are multiple longest palindromes that can be formed, return the one among them that's lexicographically smallest.

Examples :

For s = "aaabb", the output should be solution(s) = "ababa".

There are two possible palindromes of length 5 that can be obtained ("ababa" and "baaab"), but "ababa" is lexicographically smaller, thus it is the answer.

For s = "aaabbbcc", the output should be solution(s) = "abcacba".

It's not possible to form a palindrome of length 8, but from several palindromes of length 7, "abcacba" is the lexicographically smallest, thus it is the answer.

Other Examples:

Input:
s: "lollipop"
Expected Output: "lopipol"

Input:
s: "gfagcaabdfdgfaabeabbage"
Expected Output: "aaabbdefggaggfedbbaaa"

Comments (5)