3
int minDeletions(string s) {4
// Array to store the count of each character.5
vector<int> freq(26, 0);7
// Calculatimg frequency of all characters.12
// sorting the frequencies. So the greatest frequencies are in right side.13
sort(freq.begin(), freq.end());15
int del = 0; // to store the deletions.17
// Checking if 2 frequencies are same, if same then decrease the frequency18
// so that it becomes less than the next greater one.So Start from the 2nd19
// greatest frequency that is at freq[24].20
for (int i = 24; i >= 0; i--) {21
if (freq[i] == 0) break; // if frequency is 0 that means no more character is left.23
if (freq[i] >= freq[i + 1]) {24
int prev = freq[i]; // To store the frequency before deletion.25
freq[i] = max(0, freq[i + 1] - 1); // New frequency should be 1 less than the26
// previous frequency in the array.27
del += prev - freq[i]; // Calculating deleted characters