1647.cpp (507B)
1 class Solution { 2 public: 3 int minDeletions(const string &s) { 4 int count[27] = {0}; 5 for (const char c : s) 6 count[c & 0x1F]++; 7 sort(rbegin(count), rend(count)); 8 9 int res = 0; 10 for (int i = 1; i <= 26 && count[i]; i++) { 11 const int diff = count[i] - count[i - 1] + 1; 12 if (diff >= 0) { 13 res += min(count[i], diff); 14 count[i] -= min(count[i], diff); 15 } 16 } 17 return res; 18 } 19 };