题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
/*
1.找出出现次数最少的字符,用unordered_map和pair
2.删除找到的字符串,在字符串中删除特定字符的方式是
str.erase(remove(str.begin(),str.end(),[char,被删除的字符的迭代器]),str.end());
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
string a;
while (cin >> a) {
map<char, int> umci;
for(const char e : a) {
if (umci.find(e) == umci.end()) {
umci.insert({e, 1});
} else {
umci[e]++;
}
}
// 字符指定总长度+1
int min;
// 最少次数 min
for (const auto e : umci) {
min = std::min(min, e.second);
}
// get chars that second is min
for (auto it = umci.begin();it != umci.end();it++) {
if (min == it->second) {
char ch1 = it->first;
// 在字符串中删除指定字符的特定方式
a.erase(remove(a.begin(), a.end(), it->first), a.end());
}
}
cout << a << endl;
}
}
查看22道真题和解析