Introduction to Problem Statement
Given a string s and an integer k ( k<=s.length() ). Our task is to find the lexicographically largest string after removing exactly k characters from the given string.
Sample Examples
Example 1:
Input: s = codingninjas, k = 2
Output: oingninjas
Example 2:
Input: s = chunmun, k = 3
Output: unun
To solve the above problem, we have to know about lexicographic order.
What is lexicographic order
It simply means “dictionary order,” i.e. the way in which the words are arranged in a dictionary.
The word which appeared later is said to be lexicographically larger than the word which appeared earlier in the dictionary. If we want to know from the two words which word will appear before the other, we compare the words letter by the letter starting from the first position.
For example, the word “rahul” will appear before (and considered lexicographically smaller) the word “ramu” in the dictionary because the first two letters are the same, but the third letter in the word “rahul” is “h”, which is smaller than the third letter in the word “ramu” which is “m”.
Approach
The idea is simple, we create an empty res string and start traversing the string s.
If the res string is empty, we push the current character from string s in the res string.
We check for the last character of the res string if its ASCII value is smaller than the current character of the string s, we pop the last character while it is not empty, and after every removal, we reduce the value of k by 1 as we don’t remove more than k characters.
Else we push the current character in the res string.
Finally, if we removed exactly k characters from the res string, i.e. k = 0, return the res string. Else we remove the last k characters from the res string and return the res string.
Steps:
- Traverse the string s.
- We check for every character in string s; if the ASCII value of the last character of the res string is less than the current character of the string s, we remove the last character from the res string and reduce the value of k by 1.
- Now, we insert the current character from string s to res string.
- After complete traversal of string s, if k>0, we remove the last k characters from the res string as we need to remove exactly k characters.
-
Return the res string, which is the required lexicographically largest string after removing k characters.
Example:
Input = chunmun, k= 3
Initially, s = chunmun, k = 3, res = “”
Steps:
-
Push “c” in res string as res is empty, s[0] = c, res = c, k = 3.
-
Now, remove “c” from the res string because the ASCII value of “c” is smaller than the current character in string s and reduce k by 1, s[1] = h, res = h, k = 2.
-
Similarly, remove “h” and push “u” as the ASCII value of “h” is smaller than “u” and reduce k by 1, s[2] = u, res = u, k = 1.
-
Push “n” in res string because the last character’s ASCII value in res string is larger than “n”, s[3] = n, res = un, k = 1.
-
Push “m” in res string, s[4] = m, res = unm, k = 1.
-
Remove “m” from the res string and push “u”, which has more ASCII value than “m” and reduce k by 1, s[5] = u, res = unu, k = 0.
- Push “n” in the res string, s[5] = n, res = unun, k = 0.
Finally, return the res string, which is the required lexicographically largest string.