题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
#include <iostream>
#include <map>
#include <string>
#include <algorithm> // 引入algorithm以使用min函数
using namespace std;
int main() {
    map<char, int> cnt; // 用于统计每个字符出现的次数
    string s;
    cin >> s; // 读取输入的字符串
    // 统计每个字符出现的次数
    for (auto c : s) {
        cnt[c]++;
    }
    int mn = s.length(); // 初始化为最长长度,用于寻找最小出现次数
    // 寻找出现次数最少的字符出现的次数
    for (auto& x : cnt) {
        mn = min(mn, x.second);
    }
    // 输出删除出现次数最少的字符后的字符串
    for (auto c : s) {
        if (cnt[c] != mn) {
            cout << c; // 只输出出现次数不是最少的字符
        }
    }
    cout << endl; // 输出换行符
    return 0;
}
首先读取一个字符串,然后统计每个字符出现的次数。接着,找出出现次数最少的字符出现的次数,并在最后的循环中跳过这些字符,输出剩余的字符。
实现了删除字符串中出现次数最少的字符的功能。
 投递中国电信等公司10个岗位
投递中国电信等公司10个岗位