题解 | 删除字符串中出现次数最少的字符
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
const int N = 26;
// 数组的值存的是字母出现的次数
array<char, N> a;
int main() {
string s;
cin >> s;
for(const auto &c : s){
a[c - 'a'] ++ ;
}
int charIdx = 0, minValue = 20;
for(int i = 0; i < N; i ++ ){
if(a[i] != 0 && a[i] < minValue){ // 找最小的出现次数
minValue = a[i];
charIdx = i;
}
}
unordered_set<char> S;
for(int i = 0; i < N; i ++ ){ // 找与最小次数相等的字母
if(a[i] != 0 && a[i] == minValue){
S.insert(i + 'a');
}
}
string res;
for(const auto &c : s){
if(!S.count(c)){
res.push_back(c);
}
}
cout << res << "\n";
return 0;
}
// 64 位输出请用 printf("%lld")
一遍过0.0


