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

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

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

import java.io.*;
import java.util.*;

public class Main{
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String s = sc.nextLine();
            char[] cc = s.toCharArray();
            int[] count = new int[26];

            for(char c : cc){
                count[c - 'a'] += 1;
            }
            int minValue = Integer.MAX_VALUE;

            for(int i = 0; i < 26; ++i){
                if(count[i] != 0 && minValue > count[i]){
                    minValue = count[i];
                }
            }
            Set<Character> set = new HashSet<>();

            for(int i = 0; i < 26; ++i){
                if(count[i] == minValue){
                    set.add((char)('a' + i));
                }
            }
            for(char c : cc){
                if(!set.contains(c)){
                    System.out.print(c);
                }
            }

            System.out.println();
        }
    }
}
全部评论

相关推荐

01-07 15:50
四川大学 Java
看日出看日落:好好背八股,做算法。我身边跟你bg差不多的基本都大厂暑期
点赞 评论 收藏
分享
牛客鼠:校友你这简历基本无敌了,春招刷刷题去冲大厂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务