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

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

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

import java.util.*;
import java.util.regex.Pattern;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String next = scanner.nextLine();
            LinkedHashMap<String, Integer> hm = new LinkedHashMap<>();
            Pattern p = Pattern.compile("[a-z]");
            Set<Integer> hs = new HashSet<>();
            if (next.length()>20){
                break;
            }else if (p.matcher(next).find()){
                String[] split = next.split("");
                int value=1;
                for (int i = 0; i < split.length; i++) {
                    String key = split[i];
                    boolean b = hm.containsKey(key);
                    if (b){
                        int i1 = hm.get(key) + 1;
                        hm.put(key,i1);
                    }else {
                        hm.put(key,value);
                    }
                }
                for (int i = 0; i < split.length; i++) {
                    hs.add(hm.get(split[i]));
                }
//                System.out.println(hs);
//                System.out.println(Collections.min(hs));
                Integer min = Collections.min(hs);
                int i1 = min.intValue();
                String s="";
                for (int i = 0; i < split.length; i++) {
                    if (hm.get(split[i])==i1){
                        continue;
                    }else {
                        s=s+split[i];
                    }
                }
                System.out.println(s);
            }
//            Iterator<Map.Entry<String, Integer>> it = hm.entrySet().iterator();
//            while (it.hasNext()){
//                System.out.println(it.next());
//            }
        }
    }
}

全部评论

相关推荐

牛客154160166号:9月底还给我发短信,好奇怪,我24届的
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务