题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
#include <climits>
#include <iostream>
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
int main() {
string str;
cin >> str;
unordered_map<char, int> cnts;
for (char ch : str) {
++cnts[ch];
}
int min_cnt = INT_MAX;
for (auto &[_, cnt] : cnts) {
min_cnt = min(min_cnt, cnt);
}
// cout << "min cnt = " << min_cnt;
set<char> del;
for (auto &[ch, cnt] : cnts) {
if (cnt == min_cnt) {
del.insert(ch);
}
}
string result;
for (char ch : str) {
if (!del.count(ch)) {
result.push_back(ch);
}
}
cout << result << endl;
}
// 64 位输出请用 printf("%lld")
360集团公司氛围 407人发布