题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
#include #include #include<unordered_map> #include using namespace std;
int main() { string s, m; getline(cin, s); unordered_map<char, int> maps; set sets; for (char c : s) {//统计出现次数 maps[c]++; } for (auto c = maps.begin(); c != maps.end(); c++) { //按照次数排序 sets.insert(c->second); } auto sss = sets.begin(); int least = *sss;//找到低频数字 for (auto c = maps.begin(); c != maps.end(); c++) { if (c->second == least) { m.push_back(c->first); } } for (char che : m) { for (auto c = s.begin(); c < s.end(); c++) {
if (*c == che)
{
s.erase(c);
c--;
}
}
}
cout << s << endl;
return 0;
}