Given a string S consisting of only lowercase letters, the task is to find the lexicographically largest string that can be obtained by removing K characters from the given string.
Input: s = “zyxedcba”, K=1
Explanation: Character with smallest ASCII value from the given string is ‘a’.
Removal of ‘a’ generates the lexicographically largest possible string.
Input: s = “abcde”, K=2
The idea is to use Stack Data Structure to solve the problem. Follow the steps below to solve the problem:
- Traverse the string.
- For every character, check if it is greater than the character at the top of the stack. If found to be true, pop the top element of the stack if K > 0.
- Insert the character into the stack.
- After complete traversal of the string, if K > 0, then remove the top K elements of the stack.
- Finally, store the characters in the stack as the answer. Print the answer.
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.
- Maximum length of balanced string after swapping and removal of characters
- Longest palindromic string possible after removal of a substring
- Lexicographically largest string formed from the characters in range L and R
- Lexicographically largest string possible in one swap
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Largest string obtained in Dictionary order after deleting K characters
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Largest palindromic string possible from given strings by rearranging the characters
- Find the lexicographically largest palindromic Subsequence of a String
- Lexicographically largest sub-sequence of the given string
- Lexicographically largest string for given dictionary order
- Lexicographically smallest string obtained after concatenating array
- Lexicographically smallest string after M operations
- Swap all occurrences of two characters to get lexicographically smallest string
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Minimize the maximum minimum difference after one removal from array
- Count of strings that become equal to one of the two strings after one removal
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.