题解 | #删除字符串中出现次数最少的字符#

删除字符串中出现次数最少的字符

https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9

#include <iostream>
#include <map>
#include <set>
using namespace std;

int main() {

    string s;
    set<char> leastSet;

    cin >> s;

    map<char,int> m;//保存字符出现次数
    for(const char& c : s){
        m[c]++;
    }

    int count = s.size();
    for(auto& p : m){
        if(p.second < count){
            count = p.second;
        }
    }

    for(auto& p : m){
        if(p.second == count){
            leastSet.insert(p.first);
        }
    }

    string newStr;
    for(int i=0;i<s.size();i++){
        const char& c = s[i];
        if(leastSet.find(c)==leastSet.end()){
            newStr.append(1, c);
        }
    }

    cout<<newStr<<endl;

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务