MicrosoftOA #45
Min Num Letters
EasyJudge-ready
Topics (AI-labelled)Subsequences
Given a word consisting only of lowercase English letters, find the minimum number of letters to delete so that the remaining letters, in their original relative order, form a lexicographically non-decreasing string. The remaining letters do not have to be contiguous, and equal letters may all be kept.
Input
One line containing a non-empty string S. 1≤|S|≤100000, and S contains only a-z.
Output
Output the minimum number of letters to delete.
Example 1
Input:
banana
Output:
3
Example notes
The kept part must be a non-decreasing subsequence of the original string. The minimum number of deletions equals |S| minus the length of the longest non-decreasing subsequence.
Editorial
Loading solution…