题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
#include <climits> #include <iostream> #include <map> #include <vector> #include <algorithm> using namespace std; typedef pair<int,char> T; vector<T> SortByVal(map<char, int> hashMap) { vector<T> vec; for (auto it = hashMap.begin(); it != hashMap.end(); it++){ vec.push_back(make_pair( it->second, it->first)); } sort(vec.begin(), vec.end()); return vec; } int main() { map<char,int> hashMap; string s; getline(cin,s); int min = INT_MAX; for (int i = 0; i < s.length(); i++){ auto it = hashMap.find(s[i]); if (it == hashMap.end()){ hashMap[s[i]] = 1; } else{ hashMap[s[i]] += 1; } } vector<T> vec = SortByVal(hashMap); for (int i = 0; i < vec.size(); i++){ bool flag = true; while (flag){ int j = s.find(vec[i].second); if (j >= 0){ s.erase(s.begin() + j); } else{ flag = false; } } if (vec[i].first != vec[i+1].first) break; } cout << s << endl; return 0; }