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

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

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

全部评论
点赞 回复 分享
发布于 02-20 22:09 北京

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务