删除一个字符串中出现次数最少的字符
来源:互联网 发布:ps4 淘宝 送游戏 编辑:程序博客网 时间:2024/06/10 20:49
【输入】s为输入字符串,Len为输入字符串长度
如输入字符串为“abcdd”,输出为”dd”
字符串中只有小写字母,不含空格且字符串最大长度不超过20
下面贴出程序和想法:
1、第一是如果遇到大于最小字母个数的字母直接存储到字符串中,这样就可以删除字符了。
#include <stdio.h>#include <string.h>char *deClear(char *str,int len){ int i; int minnum; int count[26] = {0}; static char pstr[100]; for(i = 0; i < len; i++) { count[str[i] - 'a']++; } for( i = 0; i < len; i++) { if(count[i] != 0) { minnum = count[i]; } } for(i = 0; i < len; i++) { if(count[i] < minum && count[i] != 0) { minnum = count[i]; } } for(i = 0; i < len; i++) { if(count[str[i] - 'a'] > minnum) { pstr[n++] = str[i]; } pstr[n] = '\0'; } return pstr;}int main(){ char str[100]; int count[26] = {0}; printf("please input the string.h\n"); scanf("%s",str); int len = strlen(str); deClear(str,count,len); printf("the result is %s\n",str); reuturn 0;}
2、直接处理最小的字母。
#include <stdio.h>void delete_ch(char *str);{ int i; int len = strlen(str); for(i = 0; i < len; i++) { *(str + i) = *(str + i + 1); } *(str + len - 1) = '\0';}void get_count_ch(int *count,char *str){ int i = 0; int len = strlen(str); for(i = 0; i < len; i++) { count[*(str +i ) - 'a']++; } }int get_min_count_ch(int *count){ int i; int minnum; for(i = 0; i < 26; i++) { if(count[i] != 0) { minnum = count[i]; for(; i < 26; i++) { if(count[i] < minnum && count[i] != '\0') { minnum = count[i]; } } } } return minnum;}void delete_min_count_ch(char *str,int minnum);{ while(*str != '\0') { if(count[*str - 'a'] == minnum) { delete_ch(str); } else { str++; } }}int main(){ char str[100]; int count[26] = {0}; printf("please input the string:\n"); scanf("%s",str); int len = strlen(str); get_count_ch(count,str,len); int min = get_min_count_ch(count); delete_min_count_ch(str,min); printf("the result is %s\n",str); return 0;}
0 0
- 删除一个字符串中出现次数最少的字符
- 删除一个字符串中出现次数最少的字符
- 删除一个字符串中出现次数最少的字符
- 编写一个函数,删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除。
- 算法分析---删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- HWOJ 删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 华为oj 删除字符串中出现次数最少的字符
- 华为OJ 删除字符串中出现次数最少的字符
- (java)删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 初学构建小项目之仓库管理系统主页面的实现(二)
- Struts笔记
- 连续子数组的最大和
- useradd和adduser的区别
- Leetcode 之 MissingNumber
- 删除一个字符串中出现次数最少的字符
- Java之协变返回类型理解和简单实例
- Web请求处理
- LeetCode 396. Rotate Function
- phonegap 安装及环境配置(简介)
- 小雷会员俱乐部,加入就对了!
- 当前用户不在sudoers文件中
- 图形中添加注释(annotate)
- Android框架之Gson解析