Given a string S consisting of decrease case letters and an integer Okay, the duty is to take away minimal variety of letters from the string, such that the sum of alphabetic ordering of the letters current within the string is at most Okay.
Examples :
Enter: S = “abca”, Okay = 2
Output: “aa”
Rationalization: Preliminary sum for the given string is 1 + 2 + 3 + 1 = 7
(since positions of a, b, and c are 1, 2, and three respectively).
If we take away b and c from the string “abca”, it turns into “aa”,
having price 2 which is lower than or equal to the given Okay.Enter: S = “geeksforgeeks”, Okay= 27
Output: “eefee”
Method: The issue may be simply solved by a grasping method.
The principle statement is that first, we should take away the characters having most price earlier than eradicating the characters having lesser price.
- First create a variable (say totalCost), to retailer the entire price of the preliminary string S.
- Then create a replica of string S (say temp) and kind it in reverse order of characters.
- Traverse by way of temp and preserve storing the characters in a map (say del), until totalCost is bigger than Okay.
- These characters saved within the map are mainly the characters to be deleted.
- Lastly, traverse by way of the string S, delete the characters saved within the map del, and return the ultimate string.
Under is the implementation of the above method:
C++
|
Time Complexity: O(N * log(N)) the place N is the size of the string
Auxiliary House: O(N)