Lexicographically Smallest Palindrome

You are given a string s consisting of lowercase English letters, and you are allowed to perform operations on it. In one operation, you can replace a character in s with another lowercase English letter.

Your task is to make s a palindrome with the minimum number of operations possible. If there are multiple palindromes that can be made using the minimum number of operations, make the lexicographically smallest one.

A string a is lexicographically smaller than a string b of the same length if, in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b.

Return the resulting palindrome string.

Example 1
Inputs = "egcfe"
Output"efcfe"
The minimum number of operations is 1, and changing g produces the lexicographically smallest palindrome efcfe.
Example 2
Inputs = "abcd"
Output"abba"
The minimum number of operations is 2, and changing characters to form abba gives the lexicographically smallest palindrome.

Constraints

  • 1 <= s.length <= 1000
  • s consists of only lowercase English letters.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate