↓Skip to main content
  1. LeetCode/

LeetCode 2697: Lexicographically Smallest Palindrome

·2 mins· ·
LeetCode Easy Two-Pointers String Greedy
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-01-24

  • Submissions: Accepted (first try)

Approach
#

Make the string a palindrome with the fewest changes, and among those, the lexicographically smallest.

A palindrome needs every pair (i, n - 1 - i) to match. For each pair:

  • Already equal: no change.
  • Different: at least one change is required, and changing to the smaller character gives the smallest result.

Pairs are independent, so both goals are achieved together.

My Solution
#

class Solution:
    def makeSmallestPalindrome(self, s: str) -> str:
        chars = list(s)
        left, right = 0, len(chars) - 1
        while left < right:
            smaller = min(chars[left], chars[right])
            chars[left] = chars[right] = smaller
            left += 1
            right -= 1
        return "".join(chars)

My original handled “left is smaller” and “right is smaller” separately with if / elif; this version takes min directly.

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Changing to the larger character: the same number of changes, but a larger result.
  • Changing both sides: only the larger side needs to change.

Interview Explanation
#

A palindrome needs every symmetric pair to match, so I walk converging pointers pair by pair: when they differ, at least one must change, and changing to the smaller character keeps the change count minimal and the result smallest. Pairs do not affect each other, so time is O(n).

Related

LeetCode 942: DI String Match
·1 min
LeetCode Easy Array Two-Pointers String Greedy
LeetCode 942 note: two pointers hold the smallest and largest unused values; place the smallest on I and the largest on D
LeetCode 680: Valid Palindrome II
·2 mins
LeetCode Easy Two-Pointers String Greedy
LeetCode 680 note: at the first mismatch the only options are deleting the left or the right character; check whether either remainder is a palindrome
LeetCode 1616: Split Two Strings to Make Palindrome
·3 mins
LeetCode Medium Two-Pointers String Greedy
LeetCode 1616 note: greedily match a’s prefix against b’s suffix from the outside in; the leftover middle must be a palindrome entirely within a or b, and both directions need checking
LeetCode 3794: Reverse String Prefix
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 3794 note: one line with slicing; a hand-written two-pointer version must convert the string to a list first
LeetCode 3750: Minimum Number of Flips to Reverse Binary String
·2 mins
LeetCode Easy Two-Pointers Bit-Manipulation String
LeetCode 3750 note: compare the binary string with its reverse bit by bit; the number of mismatches is the answer
LeetCode 3545: Minimum Deletions for At Most K Distinct Characters
·1 min
LeetCode Easy Greedy Hash-Table String Sorting Counting
LeetCode 3545 note: reducing the number of kinds means deleting whole kinds, starting from the least frequent