![]() |
VOOZH | about |
Given a string s and an integer K, the task is to remove K characters from the string such that the sum of ASCII (American Standard Code for Information Interchange) values of the remaining characters is maximized.
Examples:
Input: s = "word", K = 2
Output: 233
Explanation: We need to remove exactly 2 characters from the string "word" to maximize the ASCII sum of the remaining characters. By removing 'o' and 'd', we obtain the maximum sum of 233, which corresponds to the ASCII sum of the remaining characters ('r' and 'o').Input: s = "abcABd", K = 3
Output: 297
Explanation: We need to remove exactly 3 characters from the string "abcABd" to maximize the ASCII sum of the remaining characters. By removing 'a', 'A', and 'B', we obtain the maximum sum of 297, which corresponds to the ASCII sum of the remaining characters ('b', 'c', and 'd').
Approach: Bruteforce Approach
This can be solved with the following idea:
This can be solved by keeping some observations in the notice.
Below are the steps involved in the implementation:
Below is the implementation of the above approach:
297
Time Complexity: O(2^n * n)
Auxiliary Space: O(1)
Approach: Greedy Approach
This can be solved with the following idea:
If we remove minimum ascii values characters then we will get maximized sum of remaining character.
Below is the implementation of the above approach:
297
Time Complexity: O( n*log(n) )
Auxiliary Space: O(n)