Mid/SeniorGreedyString

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
Inputs = "cbabc"
Output"baabc"
Performing the operation on the substring from index 0 to index 1 inclusive changes "cbabc" to "baabc".
Example 2
Inputs = "aa"
Output"az"
Performing the operation on the last letter changes "aa" to "az".

Constraints

  • 1 <= s.length <= 3 * 10^5
  • s consists of lowercase English letters

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate