题解 | #字符统计#

字符统计

https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0

import java.util.*;

/**
 * @author hll[yellowdradra@foxmail.com]
 * @since 2023-04-01 10:59
 **/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char[] chars = in.nextLine().toCharArray();
        Map<Character, Integer> counter = new HashMap<>(chars.length);
        for (char c : chars) {
            counter.compute(c, (k, v) -> v == null ? 1 : ++v);
        }
        Set<Map.Entry<Character, Integer>> set = new TreeSet<>((e1, e2) -> e2.getValue() - e1.getValue() == 0 ?
                e1.getKey() - e2.getKey() : e2.getValue() - e1.getValue()
        );
        set.addAll(counter.entrySet());
        set.forEach(e -> System.out.print(e.getKey()));
    }
}

全部评论

相关推荐

05-23 20:31
已编辑
武汉大学 Java
内向的柠檬精在研究求职打法:注意把武大标粗标大 本地你俩不是乱杀
点赞 评论 收藏
分享
机械打工仔:第一位颇有孟德之志
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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