12. Minimum Deletions to Make Character Frequencies Unique
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
We need to count the frequencies of each character and then adjust those frequencies to ensure uniqueness while minimizing deletions.
Count the frequency of each character in the input string 's' using a Counter object.
Initialize a variable 'deletions' to keep track of the number of deletions needed.
Create an empty set freqSet
to store unique frequencies encountered.
Iterate through each character and its frequency in chars
.
For each character, enter a loop while its current frequency is greater than 0 and it is already in chars
.
Decrement the frequency by 1, indicating that we are considering deleting one occurrence of this character.
Increment 'deletions' by 1, as we are performing a deletion operation.
Add the current frequency to freqSet
to mark it as seen.
After processing all characters, return the total number of deletions needed
Time Complexity: O(n)
Auxiliary Space Complexity: O(n)
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.