FastPrepAlphabetically Smallest Palindrome
Problem · String

Alphabetically Smallest Palindrome

Learn this problem
MediumIBM logoIBMINTERNNEW GRADOA
See IBM hiring insights

Problem statement

A palindrome reads the same from either direction; for example, ada is a palindrome. You are given a string s of lowercase English letters.

Change the fewest letters possible so that the characters of s can be rearranged to form a palindrome. If multiple palindromes require the same minimum number of changes, return the alphabetically smallest one.

Function

makeAlphabeticallySmallestPalindrome(s: String) → String

Examples

Example 1

s = "azzzbbb"return = "abzbzba"

Change one z to a, producing the letters in aazzbbb. They can be rearranged as abzbzba, the alphabetically smallest palindrome obtainable with one change.

Example 2

s = "fhaigh"return = "afhhfa"

Change i to a and g to f. The resulting pairs form the alphabetically smallest palindrome afhhfa.

Constraints

  • 1 ≤ s.length ≤ 3 × 10^5
  • s consists only of lowercase English letters.

More IBM problems

drafts saved locally
public String makeAlphabeticallySmallestPalindrome(String s) {
    // write your code here
}
s"azzzbbb"
expected"abzbzba"
checking account