Given string str of length N and an integer K, the task is to return the largest string in Dictionary Order by erasing K characters from that string.
A largest string Dictionary order is the last string when strings are arranged in alphabetical order.
Input: str = “ritz” K = 2
There are 6 possible ways of deleting two characters from s: “ri”, “rt”, “rz”, “it”, “iz”, “tz”.
Among these strings “tz” is the largest in dictionary order.
Thus “tz” is the desired output.
Input: str = “jackie” K = 2
The characters “a” and “c” are deleted to get the largest possible string.
Naive Approach: The idea is to find all the subsequence of the given string length N – K. Store those subsequences in a list. There will be nCm Such sequences. After the above steps print the largest string in alphabetical order stored in the list.
Time Complexity: O(2N-K)
Efficient Approach: The idea is to use a Deque. Below are the steps:
- Store all the characters of the string in the deque.
- Traverse the given string and for each character in the string keep popping the characters from deque if it is less than the last character stored in the deque. Perform this operation until K is non-zero.
- Now after the above operations, insert the current character in the deque.
- After the above operations, the string formed by the characters stored in the deque is the resultant string.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find largest word in dictionary by deleting some characters of given string
- Find the largest after deleting the given elements
- Count of characters in str1 such that after deleting anyone of them str1 becomes str2
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Minimum steps to delete a string by deleting substring comprising of same characters
- Determine the winner of a game of deleting Characters from a String
- Lexicographically largest string for given dictionary order
- Find i'th Index character in a binary string obtained after n iterations
- Lexicographically smallest string obtained after concatenating array
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find the k smallest numbers after deleting given elements
- Lexicographically largest possible String after removal of K characters
- Given a sorted dictionary of an alien language, find order of characters
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Maximum array sum that can be obtained after exactly k changes
- Find the number obtained after concatenation of binary representation of M and N
- Array obtained by repeatedly reversing array after every insertion from given array
- Check if a string can become empty by recursively deleting a given sub-string
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be obtained by rotating another string d places
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.