题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
int main() {
string s;
cin>>s;
unordered_map<char, int> map;
for(auto ch : s)
{
if(map.find(ch) == map.end())
map[ch] = 0;
else
map[ch] += 1;
}
vector<pair<char, int>> sorted(map.begin(), map.end());
sort(sorted.begin(), sorted.end(),[](const auto p1, const auto p2)
{
return p1.second < p2.second;
});
int n = sorted[0].second;
for(auto p: sorted)
{
if(p.second == n)
{
s.erase(remove(s.begin(), s.end(), p.first), s.end());
}
}
cout<<s;
}
// 64 位输出请用 printf("%lld")