Skip to main content
Back to problems
#2697
Easy Algorithms

Lexicographically smallest palindrome

Two Pointers String Greedy
80.9% acceptance
Feb 25, 2026
407
28
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. Return the resulting palindrome string.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn make_smallest_palindrome(s: String) -> String {
    let mut chars: Vec<u8> = s.into_bytes();
    let n = chars.len();
    for i in 0..n / 2 {
      let j = n - 1 - i;
      let m = chars[i].min(chars[j]);
      chars[i] = m;
      chars[j] = m;
    }
    String::from_utf8(chars).unwrap()
  }
}