Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to count minimum deletions needed to make character frequencies unique in Python
Given a string s, we need to make it "good" by ensuring no two different characters have the same frequency. A good string has unique frequencies for each character. We must find the minimum number of character deletions required.
For example, if s = "ssstttuu", we have 3 's', 3 't', and 2 'u' characters. Since both 's' and 't' have frequency 3, we need to delete characters to make frequencies unique.
Approach
The strategy is to count character frequencies, sort them, and reduce duplicate frequencies by deleting characters ?
- Count frequency of each character using Counter
- Sort frequencies in ascending order
- For each duplicate frequency, reduce it by 1 and increment deletion count
- Handle cascading duplicates that may occur after reduction
Example
Let us see the implementation to understand better ?
from collections import Counter
def solve(s):
val = Counter(s)
res = 0
numlist = sorted([i for i in val.values()])
for i in range(len(numlist)-1):
if numlist[i] and numlist[i] == numlist[i+1]:
numlist[i] -= 1
res += 1
k = i-1
m = i
while numlist[m] and numlist[m] == numlist[k]:
numlist[k] -= 1
k -= 1
m -= 1
res += 1
return res
s = "ssstttuu"
print(solve(s))
The output of the above code is ?
2
How It Works
For string "ssstttuu" ?
- Character frequencies: {'s': 3, 't': 3, 'u': 2}
- Sorted frequencies: [2, 3, 3]
- Found duplicate frequency 3 at positions 1 and 2
- Reduce frequency at position 1 from 3 to 2, increment deletions
- Now we have [2, 2, 3] ? another duplicate at positions 0 and 1
- Reduce frequency at position 0 from 2 to 1, increment deletions
- Final frequencies: [1, 2, 3] ? all unique
- Total deletions: 2
Alternative Approach
A cleaner solution using a greedy approach ?
from collections import Counter
def solve_optimized(s):
frequencies = Counter(s)
freq_counts = sorted(frequencies.values(), reverse=True)
deletions = 0
for i in range(1, len(freq_counts)):
if freq_counts[i] >= freq_counts[i-1]:
target_freq = max(0, freq_counts[i-1] - 1)
deletions += freq_counts[i] - target_freq
freq_counts[i] = target_freq
return deletions
s = "ssstttuu"
print(solve_optimized(s))
2
Conclusion
To make character frequencies unique, count frequencies and greedily reduce duplicates from highest to lowest. The optimized approach processes frequencies in descending order, ensuring each frequency is strictly less than the previous one.
