Minimizing the String

You are given a string s consisting of n lowercase English letters.

You are allowed to remove at most one character (i.e., remove zero or one character) from the string.

Return the lexicographically smallest string that can be obtained after performing this operation.

A string a is lexicographically smaller than string b if:

  • At the first position where they differ, the character in a comes before the character in b in the alphabet.
  • If one string is a prefix of the other, the shorter string is smaller.

Examples:

  • "aaa" is smaller than "aaaa"
  • "abb" is smaller than "abc"
  • "pqr" is smaller than "z"

Example 1:

Input: s = "cbad"

Output: "bad"

Explanation:

If we remove 'c', we get "bad", which is smaller than removing any other character.

Example 2:

Input: s = "abdc"

Output: "abc"

Explanation:

Removing 'd' results in "abc", which is lexicographically smallest.

Now Your Turn!

Pick the correct output for the given input

Input: s = "aaa"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 ≤ n ≤ 2 × 105
  • s consists only of lowercase English letters ('a' to 'z')
  • You may remove at most one character

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
string minimizeString(string s) {
// Your code goes here
}
};
Test Case

Input:

S