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

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

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Stream.*;


public class Main {
    public static void main(String[] args) throws IOException {

        //testCompletePack();
        testTh();
    }

    private static void testTh() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;
        HashMap<Character, Integer> hp = new HashMap<>();
        int count = 0;
        int min = Integer.MAX_VALUE;
        while ((str = bf.readLine()) != null) {
            char[] chars = str.toCharArray();
            char[] temp = new char[chars.length];
            for (int i = 0; i < chars.length; i++) {
                if (hp.containsKey(chars[i])) {
                    count = hp.get(chars[i]);
                    count++;
                    hp.put(chars[i], count);
                } else {
                    hp.put(chars[i], 1);
                }
            }
            Set<Map.Entry<Character, Integer>> entries = hp.entrySet();
            int i = 0;
            for (Map.Entry<Character, Integer> entry : entries) {
                if (entry.getValue() < min) {
                    min = entry.getValue();
                    temp[i] = entry.getKey();
                }
            }
            i++;
            for (Map.Entry<Character, Integer> entry : entries) {
                if (entry.getValue() == min) {
                    temp[i] = entry.getKey();
                    i++;
                }
            }

            for (int i1 = 0; i1 < temp.length; i1++) {
                String s = String.valueOf(temp[i1]);
                str = str.replaceAll(s, "");
            }
            Stream.of(str).forEach(System.out::println);
        }
    }

}

全部评论
我宣布以后我会常来大佬主页溜达
点赞 回复 分享
发布于 2023-05-29 09:30 陕西
要是能加上注释就更好理解了😎
点赞 回复 分享
发布于 2023-05-29 09:09 湖南

相关推荐

评论
点赞
收藏
分享

创作者周榜

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