FastPrepFastPrep
Problem Brief

Get Alphabetically Smallest String

FULLTIMEOA
See Microsoft online assessment and hiring insights

Given a string S consisting of N chars, return the alphabetically smallest string that can be obtained by removing exactly one letter from S.

1Example 1

Input
S = "acb"
Output
"ab"
Explanation
By removing 1 letter, you will obtain "ac", "ab", or "cb". Your function should return "ab" (After removing 'c') since it is a alphabetically smaller than "ac" and "bc" :)

2Example 2

Input
S = "hot"
Output
"ho"
Explanation
"ho" is alphabetically smaller than "ht" and "ot"

3Example 3

Input
S = "codility"
Output
"cdility"
Explanation
Obtain the answer by removing the second letter.

4Example 4

Input
S = "aaaa"
Output
"aaa"
Explanation
Any occurrence of 'a' can be removed.

Constraints

Limits and guarantees your solution can rely on.

  • N is an integer within the range [2...100,000]
  • string S is made only of lowercase letters (a-z)
  • public String getAlphabeticallySmallestString(String S) {
      // write your code here
    }
    
    Input

    S

    "acb"

    Output

    "ab"

    Sign in to submit your solution.