LeetCode Problem

How to Solve Lexicographically Smallest Palindrome

To solve the Lexicographically Smallest Palindrome problem, use two-pointer scanning to ensure the string becomes a palindrome. The challenge is to make minimal changes while ensuring the result is lexicographically smallest. This requires scanning from both ends and adjusting mismatched characters with minimal modifications.

GhostInterview Help

Need help with Lexicographically Smallest Palindrome without spending extra time grinding it?

GhostInterview can read Lexicographically Smallest Palindrome from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #2697Two-pointer scanning with invariant trackingReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Two-pointer scanning with invariant tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve the Lexicographically Smallest Palindrome problem, use two-pointer scanning to ensure the string becomes a palindrome. The challenge is to make minimal changes while ensuring the result is lexicographically smallest. This requires scanning from both ends and adjusting mismatched characters with minimal modifications.

Problem Statement

You are given a string s consisting of lowercase English letters. Your task is to make s a palindrome by performing the fewest possible operations. In each operation, you can replace a character in s with another lowercase English letter.

If there are multiple palindromes achievable with the same minimum number of operations, you must choose the lexicographically smallest palindrome. A string a is lexicographically smaller than string b if, at the first differing position, the character in a appears earlier in the alphabet than the corresponding character in b.

Examples

Example 1

Input: s = "egcfe"

Output: "efcfe"

The minimum number of operations to make "egcfe" a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is "efcfe", by changing 'g'.

Example 2

Input: s = "abcd"

Output: "abba"

The minimum number of operations to make "abcd" a palindrome is 2, and the lexicographically smallest palindrome string we can get by modifying two characters is "abba".

Example 3

Input: s = "seven"

Output: "neven"

The minimum number of operations to make "seven" a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is "neven".

Constraints

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

Solution Approach

Two-pointer approach

Use two pointers starting at both ends of the string. At each step, compare the characters at the two pointers. If they differ, replace the larger character with the smaller one to ensure the lexicographically smallest palindrome. This minimizes the number of operations by focusing on only the necessary modifications.

Greedy strategy

In addition to making the string a palindrome, prioritize making the lexicographically smallest choice by always picking the smaller character when characters mismatch. This greedy approach ensures that at every step, you are making the smallest possible change.

Optimized scanning

By scanning the string once from both ends, the solution operates in O(n) time complexity. This ensures that the solution is efficient, even for the maximum string length of 1000, while also keeping the space complexity low.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity is O(n) because each character in the string is visited once by the two-pointer approach. The space complexity is O(1) since no additional space is required beyond the input string.

What Interviewers Usually Probe

  • Tests understanding of two-pointer techniques and string manipulation.
  • Checks for the ability to implement a greedy strategy to minimize changes.
  • Evaluates efficiency in handling strings of up to 1000 characters.

Common Pitfalls or Variants

Common pitfalls

  • Over-complicating the approach by introducing unnecessary operations.
  • Failing to prioritize lexicographically smallest changes when characters mismatch.
  • Not optimizing the solution for time and space complexity, especially for larger input strings.

Follow-up variants

  • Consider an approach where the string is already a palindrome.
  • Test cases where multiple palindromes are possible with the same number of changes.
  • Explore the case where no changes are needed, testing the efficiency of the solution.

How GhostInterview Helps

  • GhostInterview guides you through understanding the two-pointer scanning technique, ensuring you handle mismatched characters optimally.
  • Our solver helps you implement a greedy strategy for making the lexicographically smallest palindrome with minimal changes.
  • GhostInterview provides efficient solutions and helps you avoid common pitfalls like unnecessary operations or inefficient space usage.

Topic Pages

FAQ

How do I solve the Lexicographically Smallest Palindrome problem?

Use two-pointer scanning to compare characters at both ends of the string. Modify the larger character to the smaller one, ensuring minimal changes.

What is the time complexity of the Lexicographically Smallest Palindrome problem?

The time complexity is O(n), where n is the length of the string, as you only scan the string once with two pointers.

How do I prioritize the lexicographically smallest palindrome?

Whenever characters mismatch, replace the larger character with the smaller one to make the result lexicographically smallest.

What if the string is already a palindrome?

If the string is already a palindrome, no changes are needed, and the output will be the same as the input string.

Can I solve this problem without using the two-pointer technique?

While other techniques are possible, the two-pointer technique is the most efficient and optimal for this problem, ensuring minimal operations.

GhostInterview Solver

Need direct help with Lexicographically Smallest Palindrome instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Lexicographically Smallest Palindrome from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.