Lexicographically Smallest Palindrome - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abcd"
›
Output:
"abba"
💡 Note:
Compare 'a' and 'd': choose 'a' for both positions. Compare 'b' and 'c': choose 'b' for both positions. Result is "abba" with 2 operations.
Example 2 — Already Palindrome
$
Input:
s = "aa"
›
Output:
"aa"
💡 Note:
String is already a palindrome, no operations needed.
Example 3 — Single Character
$
Input:
s = "a"
›
Output:
"a"
💡 Note:
Single character is always a palindrome, no operations needed.
Constraints
- 1 ≤ s.length ≤ 105
- s consists of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Input
Original string with potential mismatches
2
Process
Compare characters from both ends, fix with smaller character
3
Output
Resulting palindrome that is lexicographically smallest
Key Takeaway
🎯 Key Insight: Use two pointers and always choose the lexicographically smaller character when fixing mismatches
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code