2734. Lexicographically Smallest String After Substring Operation LeetCode Solution

In this guide, you will get 2734. Lexicographically Smallest String After Substring Operation LeetCode Solution with the best time and space complexity. The solution to Lexicographically Smallest String After Substring Operation 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 String After Substring Operation solution in C++
  4. Lexicographically Smallest String After Substring Operation solution in Java
  5. Lexicographically Smallest String After Substring Operation solution in Python
  6. Additional Resources
2734. Lexicographically Smallest String After Substring Operation LeetCode Solution image

Problem Statement of Lexicographically Smallest String After Substring Operation

Given a string s consisting of lowercase English letters. Perform the following operation:

Select any non-empty substring then replace every letter of the substring with the preceding letter of the English alphabet. For example, ‘b’ is converted to ‘a’, and ‘a’ is converted to ‘z’.

Return the lexicographically smallest string after performing the operation.

Example 1:

Input: s = “cbabc”
Output: “baabc”
Explanation:
Perform the operation on the substring starting at index 0, and ending at index 1 inclusive.

Example 2:

Input: s = “aa”
Output: “az”
Explanation:
Perform the operation on the last letter.

Example 3:

Input: s = “acbbc”
Output: “abaab”
Explanation:
Perform the operation on the substring starting at index 1, and ending at index 4 inclusive.

Example 4:

Input: s = “leetcode”
Output: “kddsbncd”
Explanation:
Perform the operation on the entire string.

Constraints:

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

See also  1307. Verbal Arithmetic Puzzle LeetCode Solution

Complexity Analysis

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

2734. Lexicographically Smallest String After Substring Operation LeetCode Solution in C++

class Solution {
 public:
  string smallestString(string s) {
    const int n = s.length();
    int i = 0;

    while (i < n && s[i] == 'a')
      ++i;
    if (i == n) {
      s[n - 1] = 'z';
      return s;
    }

    while (i < n && s[i] != 'a')
      --s[i++];

    return s;
  }
};
/* code provided by PROGIEZ */

2734. Lexicographically Smallest String After Substring Operation LeetCode Solution in Java

class Solution {
  public String smallestString(String s) {
    char[] charArray = s.toCharArray();
    final int n = s.length();
    int i = 0;

    while (i < n && charArray[i] == 'a')
      ++i;
    if (i == n) {
      charArray[n - 1] = 'z';
      return new String(charArray);
    }

    while (i < n && charArray[i] != 'a')
      --charArray[i++];

    return new String(charArray);
  }
}
// code provided by PROGIEZ

2734. Lexicographically Smallest String After Substring Operation LeetCode Solution in Python

class Solution:
  def smallestString(self, s: str) -> str:
    chars = list(s)
    n = len(s)
    i = 0

    while i < n and chars[i] == 'a':
      i += 1
    if i == n:
      chars[-1] = 'z'
      return ''.join(chars)

    while i < n and s[i] != 'a':
      chars[i] = chr(ord(chars[i]) - 1)
      i += 1

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

Additional Resources

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