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

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

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            int len = str.length();
            Map<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < len; i++) {
                if (map.containsKey(str.charAt(i))) {
                    map.put(str.charAt(i), map.get(str.charAt(i)) + 1);
                } else {
                    map.put(str.charAt(i), 1);
                }
            }
            // 找出value最小的值
            int min = Collections.min(map.values());
            // 空字符串替换
            for (Character chr : map.keySet()) {
                if (map.get(chr) == min) {
                    str = str.replaceAll(String.valueOf(chr), "");
                }
            }
            System.out.println(str);
        }
    }
}

全部评论

相关推荐

05-30 15:29
佛山大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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