返回介绍

solution / 2700-2799 / 2734.Lexicographically Smallest String After Substring Operation / README_EN

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

2734. Lexicographically Smallest String After Substring Operation

中文文档

Description

You are given a string s consisting of only lowercase English letters. In one operation, you can do the following:

  • Select any non-empty substring of s, possibly the entire string, then replace each one of its characters with the previous character of the English alphabet. For example, 'b' is converted to 'a', and 'a' is converted to 'z'.

Return _the lexicographically smallest string you can obtain after performing the above operation exactly once._

A substring is a contiguous sequence of characters in a string.

A string x is lexicographically smaller than a string y of the same length if x[i] comes before y[i] in alphabetic order for the first position i such that x[i] != y[i].

 

Example 1:

Input: s = "cbabc"
Output: "baabc"
Explanation: We apply the operation on the substring starting at index 0, and ending at index 1 inclusive. 
It can be proven that the resulting string is the lexicographically smallest. 

Example 2:

Input: s = "acbbc"
Output: "abaab"
Explanation: We apply the operation on the substring starting at index 1, and ending at index 4 inclusive. 
It can be proven that the resulting string is the lexicographically smallest. 

Example 3:

Input: s = "leetcode"
Output: "kddsbncd"
Explanation: We apply the operation on the entire string. 
It can be proven that the resulting string is the lexicographically smallest. 

 

Constraints:

  • 1 <= s.length <= 3 * 105
  • s consists of lowercase English letters

Solutions

Solution 1

class Solution:
  def smallestString(self, s: str) -> str:
    n = len(s)
    i = 0
    while i < n and s[i] == "a":
      i += 1
    if i == n:
      return s[:-1] + "z"
    j = i
    while j < n and s[j] != "a":
      j += 1
    return s[:i] + "".join(chr(ord(c) - 1) for c in s[i:j]) + s[j:]
class Solution {
  public String smallestString(String s) {
    int n = s.length();
    int i = 0;
    while (i < n && s.charAt(i) == 'a') {
      ++i;
    }
    if (i == n) {
      return s.substring(0, n - 1) + "z";
    }
    int j = i;
    char[] cs = s.toCharArray();
    while (j < n && cs[j] != 'a') {
      cs[j] = (char) (cs[j] - 1);
      ++j;
    }
    return String.valueOf(cs);
  }
}
class Solution {
public:
  string smallestString(string s) {
    int n = s.size();
    int i = 0;
    while (i < n && s[i] == 'a') {
      ++i;
    }
    if (i == n) {
      s[n - 1] = 'z';
      return s;
    }
    int j = i;
    while (j < n && s[j] != 'a') {
      s[j] = s[j] - 1;
      ++j;
    }
    return s;
  }
};
func smallestString(s string) string {
  n := len(s)
  i := 0
  for i < n && s[i] == 'a' {
    i++
  }
  cs := []byte(s)
  if i == n {
    cs[n-1] = 'z'
    return string(cs)
  }
  j := i
  for j < n && cs[j] != 'a' {
    cs[j] = cs[j] - 1
    j++
  }
  return string(cs)
}
function smallestString(s: string): string {
  const cs: string[] = s.split('');
  const n: number = cs.length;
  let i: number = 0;
  while (i < n && cs[i] === 'a') {
    i++;
  }

  if (i === n) {
    cs[n - 1] = 'z';
    return cs.join('');
  }

  let j: number = i;
  while (j < n && cs[j] !== 'a') {
    const c: number = cs[j].charCodeAt(0);
    cs[j] = String.fromCharCode(c - 1);
    j++;
  }

  return cs.join('');
}
impl Solution {
  pub fn smallest_string(s: String) -> String {
    let mut cs: Vec<char> = s.chars().collect();
    let n = cs.len();
    let mut i = 0;

    while i < n && cs[i] == 'a' {
      i += 1;
    }

    if i == n {
      cs[n - 1] = 'z';
      return cs.into_iter().collect();
    }

    let mut j = i;
    while j < n && cs[j] != 'a' {
      cs[j] = ((cs[j] as u8) - 1) as char;
      j += 1;
    }

    cs.into_iter().collect()
  }
}

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

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

发布评论

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