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

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

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

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

int main() {
    string input;
    cin >> input;
    int freq[26] = {0}; // 记录字母出现的频率
    for(char c : input) ++freq[c - 'a'];
    // 寻找出现次数最少的字符
    int index = -1;
    set<char> targetChar;
    for(int i=0; i<26; ++i) {
        if(freq[i] == 0) continue;
        else {
            if(index == -1) {
                index = i;
                targetChar.insert('a' + i);
            }
            else if(freq[i] > freq[index]) continue;
            else if(freq[i] == freq[index]) targetChar.insert('a' + i);
            else {
                index = i;
                targetChar.clear();
                targetChar.insert('a' + i);
            }
        }
    }
    // 删除出现最少的字符targetChar
    string output;
    for(int i=0; i<input.size(); ++i) {
        if(targetChar.count(input[i]) == 0) output.push_back(input[i]);
        else continue;
    }
    cout << output;
    return 0;
}

注意出现最少的字符可能不止一种

全部评论

相关推荐

佛系的本杰明反对画饼:个人看法,实习经历那段是败笔,可以删掉,它和你目标岗位没什么关系,没有用到什么专业技能,甚至会降低你项目经历内容的可信度。个人技能那里可以再多写一点,去boss直聘上看别人写的岗位要求,可以把你会的整合一下,比如熟悉常规的开关电源拓扑结构(BUCK、正激、反激、LLC等),熟悉常用的通信总线协议和通信接口,如UART,IIC,SPI等。简历首先是HR看的,HR大多不懂技术,会从简历里去找关键字,你没有那些关键字他可能就把你筛掉了,所以个人技能尽量针对着岗位描述写一下。还有电赛获佳绩,获奖了就写什么奖,没获奖就把获佳绩删了吧,要不会让人感觉夸大。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务