返回介绍

solution / 2600-2699 / 2697.Lexicographically Smallest Palindrome / README_EN

发布于 2024-06-17 01:03:01 字数 5193 浏览 0 评论 0 收藏 0

2697. Lexicographically Smallest Palindrome

中文文档

Description

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".

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.

Solutions

Solution 1: Greedy + Two Pointers

We use two pointers $i$ and $j$ to point to the beginning and end of the string, initially $i = 0$, $j = n - 1$.

Next, each time we greedily modify $s[i]$ and $s[j]$ to their smaller value to make them equal. Then we move $i$ one step forward and $j$ one step backward, and continue this process until $i \ge j$. At this point, we have obtained the smallest palindrome string.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

class Solution:
  def makeSmallestPalindrome(self, s: str) -> str:
    cs = list(s)
    i, j = 0, len(s) - 1
    while i < j:
      cs[i] = cs[j] = min(cs[i], cs[j])
      i, j = i + 1, j - 1
    return "".join(cs)
class Solution {
  public String makeSmallestPalindrome(String s) {
    char[] cs = s.toCharArray();
    for (int i = 0, j = cs.length - 1; i < j; ++i, --j) {
      cs[i] = cs[j] = (char) Math.min(cs[i], cs[j]);
    }
    return new String(cs);
  }
}
class Solution {
public:
  string makeSmallestPalindrome(string s) {
    for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
      s[i] = s[j] = min(s[i], s[j]);
    }
    return s;
  }
};
func makeSmallestPalindrome(s string) string {
  cs := []byte(s)
  for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
    cs[i] = min(cs[i], cs[j])
    cs[j] = cs[i]
  }
  return string(cs)
}
function makeSmallestPalindrome(s: string): string {
  const cs = s.split('');
  for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
    cs[i] = cs[j] = s[i] < s[j] ? s[i] : s[j];
  }
  return cs.join('');
}
impl Solution {
  pub fn make_smallest_palindrome(s: String) -> String {
    let mut cs: Vec<char> = s.chars().collect();
    let n = cs.len();
    for i in 0..n / 2 {
      let j = n - 1 - i;
      cs[i] = std::cmp::min(cs[i], cs[j]);
      cs[j] = cs[i];
    }
    cs.into_iter().collect()
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文