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

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

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

大小为26的数组来存储每个小写字母出现的次数,然后统计出现次数最小的,替换为空

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int[] array = new int[26];
        for(int i =0;i<str.length();i++){
            char chr = str.charAt(i);
            array[chr-'a'] = array[chr-'a'] + 1;                    
        }
        int min = Integer.MAX_VALUE;
        for(int i:array){
            if(i!=0 && i<min){
                min=i;
            }
        }
        for(int i =0;i<array.length;i++){
            if(array[i]==min){
                str = str.replaceAll(String.valueOf((char)('a'+i)),"");
            }
        }
        System.out.println(str);
    }
}
全部评论

相关推荐

04-08 23:37
已编辑
东华大学 结构工程师
点赞 评论 收藏
分享
04-15 09:59
门头沟学院 C++
yy_11:小公司人家没必要泄密,大公司都是本地部署了
你想吐槽公司的哪些规定
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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