每日一道算法题 删除字符串中出现次数最少的字符

题目

删除字符串中出现次数最少的字符_牛客题霸_牛客网 (nowcoder.com)

C语言

#include <stdio.h>
#include <string.h>


void fun_2024_6_17(void) {

    char str[20] = { 0 };
    while (scanf("%s", str)!=EOF) {
        int alpha[26] = { 0 };
        int min = 20;
        int len = strlen(str);
        for (int _ = 0; _ < len; _++) alpha[str[_] - 'a']++;

        for (int _ = 0; _ < 26; _++) if (alpha[_] && alpha[_] < min) min = alpha[_];

        for (int _ = 0; _ < len;
                _++) if (alpha[str[_] - 'a'] > min) printf("%c", str[_]);

        printf("\n");
    }
}
int main() {
    /*char s[] = "1234";
    printf("%s", s + 1);*/
    fun_2024_6_17();
    return 0;
}

C++

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


void fun_2024_6_17(void) {
    string s;
    vector<int> counter(26, 0);
    while (cin >> s) {
        for (auto c : s) counter[c - 'a']++;
        int m = counter[s[0] - 'a'];
        for (auto c : s) if (counter[c - 'a']) m = min(m, counter[c - 'a']);

        for (auto c : s) if (counter[c - 'a'] > m) cout << c;

    }

}

int main() {
    fun_2024_6_17();
    return 0;
}

Python

from collections import Counter

def fun_2024_6_17(s):

    counter=Counter(s)
    minV=min(counter.values())
    for e in s:
        if counter[e]!=minV:
            print(e,end='')

s=input()
fun_2024_6_17(s)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值