2697. Lexicographically Smallest Palindrome LeetCode Solution

In this guide, you will get 2697. Lexicographically Smallest Palindrome LeetCode Solution with the best time and space complexity. The solution to Lexicographically Smallest Palindrome problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Lexicographically Smallest Palindrome solution in C++
  4. Lexicographically Smallest Palindrome solution in Java
  5. Lexicographically Smallest Palindrome solution in Python
  6. Additional Resources
2697. Lexicographically Smallest Palindrome LeetCode Solution image

Problem Statement of 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:

Input: s = “egcfe”
Output: “efcfe”
Explanation: 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”
Explanation: 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”.

See also  231. Power of Two LeetCode Solution

Example 3:

Input: s = “seven”
Output: “neven”
Explanation: 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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2697. Lexicographically Smallest Palindrome LeetCode Solution in C++

class Solution {
 public:
  string makeSmallestPalindrome(string s) {
    for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
      const int minChar = min(s[i], s[j]);
      s[i] = minChar;
      s[j] = minChar;
    }
    return s;
  }
};
/* code provided by PROGIEZ */

2697. Lexicographically Smallest Palindrome LeetCode Solution in Java

class Solution {
  public String makeSmallestPalindrome(String s) {
    char[] chars = s.toCharArray();
    for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
      final char minChar = (char) Math.min(chars[i], chars[j]);
      chars[i] = minChar;
      chars[j] = minChar;
    }
    return new String(chars);
  }
}
// code provided by PROGIEZ

2697. Lexicographically Smallest Palindrome LeetCode Solution in Python

class Solution:
  def makeSmallestPalindrome(self, s: str) -> str:
    chars = list(s)
    i = 0
    j = len(s) - 1

    while i < j:
      minChar = min(chars[i], chars[j])
      chars[i] = minChar
      chars[j] = minChar
      i += 1
      j -= 1

    return ''.join(chars)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.