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

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

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

#include <stdio.h>
#include <string.h>
//  int count 26
//str 逐字符读入数组 str[i]-'a'
//count = min  count=0;
//

int main(){
int count[26]={0};
char str[25];
while(scanf("%s",str)!=EOF){
    int count[26]={0};
    for(int i=0;i<strlen(str);i++){
        count[str[i]-'a']++;
    }
    int min=20;
    for(int i=0;i<26;i++){
        if(count[i]!=0 && count[i]<min)
            min = count[i];
    }
    
    for(int i=0;i<strlen(str);i++){
        if(count[str[i]-'a']>min)
            printf("%c",str[i]);
    }
    printf("\n");
    
    
}
    
    
    
return 0;
}
全部评论
这个思路太棒了,直接用一个数组就把字母和个数对应上,一个str[i]-'a'直接就定位了。 可以再补一个if_else排除aabb这种情况,用来保证这个字符串始终有输出。
点赞 回复 分享
发布于 2025-12-27 12:33 四川

相关推荐

点赞 评论 收藏
分享
评论
6
收藏
分享

创作者周榜

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