Given a string S consisting of lowercase characters, you have to delete the minimum number of characters from S so that every letter in S appears a unique number of times.
Input: str = “aaaabbbb”
Both ‘a’ and ‘b’ occur 4 times if the first character is deleted then ‘a’ occurs 3 times and ‘b’ occurs 4 times.
Input: str = “geeksforgeeks”
- We can count the frequencies of all 26 possible lower case characters.
- Then construct a map that tells how many letters have a given frequency.
- Iterate the map from higher frequencies to lower.
- If more than 1 letter has a given frequency we delete 1 occurrence of all but 1 letter with that frequency.
- Deleting 1 occurrence of those letters increases the number of the letter that has the frequency one less.
- If the occurrence of a letter is 0 then it is not present in the string.
Below is the implementation of the above approach:
Time Complexity: O(n + m*m), where m = 26.
- Counting the frequency of all characters takes O(N) time where N is the length of S.
- Constructing the map works in O(mlogm) time, where m = 26.
- Deletion works in O(m*m)
- Minimum deletions required to make GCD of the array equal to 1
- Minimum deletions required such that any number X will occur exactly X times
- Minimum Increment operations to make Array unique
- Minimum operations to make frequency of all characters equal K
- Minimum changes required to make a Catalan Sequence
- Minimum changes required to make two arrays identical
- Minimum number of changes required to make the given array an AP
- Minimum changes required to make all element in an array equal
- Minimum no. of operations required to make all Array Elements Zero
- Minimum number of pairs required to make two strings same
- Minimum operations required to make all the array elements equal
- Minimum array insertions required to make consecutive difference <= K
- Minimum operations required to make every element greater than or equal to K
- Minimum swaps required to make a binary string alternating
- Minimum increment or decrement operations required to make the array sorted
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.