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

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

https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9?tpId=37&tqId=21246&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

#include <bits/stdc++.h>
using namespace std;

int main() {
    string s;
    cin >> s;
    
    // 统计每个字符出现的次数
    unordered_map<char, int> freq;
    for (char c : s) {
        freq[c]++;
    }
    
    // 找到最小出现次数
    int min_freq = INT_MAX;
    for (auto& pair : freq) {
        min_freq = min(min_freq, pair.second);
    }
    
    // 构建结果字符串,跳过出现次数等于最小次数的字符
    string result = "";
    for (char c : s) {
        if (freq[c] != min_freq) {
            result += c;
        }
    }
    
    cout << result << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

02-16 01:39
南昌大学 Java
重剑Ds:感觉不太可能 后端都减飞了 根本不缺人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务