Problem · Dynamic Programming
Min Num Letters
Learn this problemProblem 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) → intExamples
Example 1
S = "banana"return = 3Because 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)