Problem · Dynamic Programming

Min Num Letters

Learn this problem
MediumMicrosoftFULLTIMEOA
See Microsoft hiring insights

Problem statement

Given a word, calculate the smallest number of letters that must be removed in order for the letters of the remaining word to be sorted in lexicographical order. The resulting word need not appear in the dictionary of any particular language.

Given string S, returns the min num of letters that must be removed.

Function

minNumLetters(S: String) → int

Examples

Example 1

S = "banana"return = 3
Because we can remove three letters (the 1st, 3rd and 6th) to get the word "aan", which is sorted. Pls note that it is not possible to remove fewer than 3 letters

Constraints

  • the length of string S is within the range [1..100,000]
  • string S consists only of lower case letters (a-z)
  • More Microsoft problems

    drafts saved locally
    public int minNumLetters(String S) {
      // write your code here
    }
    
    S"banana"
    expected3
    checking account