Given a string of lowercase alphabets and a number k, the task is to print the minimum value of the string after removal of ‘k’ characters. The value of a string is defined as the sum of squares of the count of each distinct character. For example consider the string “saideep”, here frequencies of characters are s-1, a-1, i-1, e-2, d-1, p-1 and value of the string is 1^2 + 1^2 + 1^2 + 1^2 + 1^2 + 2^2 = 9.
Expected Time Complexity: O(k*logn)
Input : str = abccc, K = 1 Output : 6 We remove c to get the value as 12 + 12 + 22 Input : str = aaab, K = 2 Output : 2
Asked In : Amazon
One clear observation is that we need to remove character with highest frequency. One trick is the character ma
A Simple solution is to use sorting technique through all current highest frequency reduce up to k times. For After every reduce again sort frequency array.
A Better Solution used to Priority Queue which has to the highest element on top.
- Initialize empty priority queue.
- Count frequency of each character and Store into temp array.
- Remove K characters which have highest frequency from queue.
- Finally Count Sum of square of each element and return it.
Below is the implementation of the above idea.
Time Complexity: O(k*logn)
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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 non-repeating characters after removing K characters
- Check if string remains palindrome after removing given number of characters
- Character replacement after removing duplicates from a string
- Minimize ASCII values sum after removing all occurrences of one character
- Print string after removing all (“10” or “01”) from the binary string
- Minimize cost to empty a given string by removing characters alphabetically
- Minimum operations required to convert all characters of a String to a given Character
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Queries for counts of array elements with values in given range
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Reduce the number to minimum multiple of 4 after removing the digits
- Minimize the length of string by removing occurrence of only one character
- Lexicographically smallest string formed by removing at most one character
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Print reverse string after removing vowels
- Balance a string after removing extra brackets
- Find the resultant String after replacing X with Y and removing Z
- Check if string is palindrome after removing all consecutive duplicates
- Ways of transforming one string to other by removing 0 or more characters
- Reduce the string by removing K consecutive identical characters