Advertisement

Lexicographically Smallest String After Substring Operation - LeetCode 2734 Solution

Lexicographically Smallest String After Substring Operation - Complete Solution Guide

Lexicographically Smallest String After Substring Operation is LeetCode problem 2734, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem asks us to find the lexicographically smallest string that can be obtained from a given string `s` by applying a specific operation. The operation involves selecting any non-empty substring and replacing each character in that substring with its preceding letter in the English alphabet (e.g., 'b' becomes 'a', and 'a' becomes 'z'). The goal is to minimize the string lexicographically, meaning we want to make it as close to 'a' as possible from left to right. The input is a string `s` of lowercase English letters, and the output is the lexicographically smallest string after performing the described operation.

Solution Approach

The solution uses a greedy approach to find the lexicographically smallest string. It iterates through the string from left to right. The algorithm attempts to find the first character that is not 'a'. Once it finds such a character, it starts converting the subsequent characters to their preceding characters until it encounters an 'a' or the end of the string. If the entire string consists of 'a' characters, it changes the last character to 'z'.

Step-by-Step Algorithm

  1. Step 1: Convert the input string `s` into a character array (or equivalent data structure for string manipulation) to allow in-place modification.
  2. Step 2: Iterate through the character array from the beginning to find the first character that is not 'a'.
  3. Step 3: If all characters are 'a' (i.e., the loop completes without finding a non-'a' character), change the last character to 'z' and return the modified string.
  4. Step 4: If a non-'a' character is found at index `i`, start from that index and continue changing characters to their preceding character until an 'a' is encountered or the end of the string is reached. The conversion is done by subtracting 1 from the ASCII value of each character.
  5. Step 5: Convert the character array back into a string and return the result.

Key Insights

  • Insight 1: The key is to realize that we want to change characters to their preceding characters ('b' -> 'a', 'c' -> 'b', etc.) as much as possible, but strategically. Converting a character to 'a' is optimal. Converting 'a' to 'z' is undesirable except for a special case.
  • Insight 2: We should prioritize changing characters to 'a' from left to right. The first non-'a' character should be the start of our substring to be converted.
  • Insight 3: If the entire string consists of 'a' characters, we only change the last character to 'z' because that will be the smallest possible string in that scenario. This handles an important edge case.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: String, Greedy.

Companies

Asked at: Agoda, IBM.