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

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

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());
//            }
        }
    }
}

全部评论

相关推荐

牛客263158796号:我领羊一面后十天不挂也不推进 今天问hr说等前序的第一批意向发完看情况再看是否推进
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务