Problem · String

Get Alphabetically Smallest String

Learn this problem
EasyMicrosoftFULLTIMEOA
See Microsoft hiring insights

Problem statement

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

Function

getAlphabeticallySmallestString(S: String) → String

Examples

Example 1

S = "acb"return = "ab"
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" :)

Example 2

S = "hot"return = "ho"
"ho" is alphabetically smaller than "ht" and "ot"

Example 3

S = "codility"return = "cdility"
Obtain the answer by removing the second letter.

Example 4

S = "aaaa"return = "aaa"
Any occurrence of 'a' can be removed.

Constraints

  • N is an integer within the range [2...100,000]
  • string S is made only of lowercase letters (a-z)
  • More Microsoft problems

    drafts saved locally
    public String getAlphabeticallySmallestString(String S) {
      // write your code here
    }
    
    S"acb"
    expected"ab"
    checking account