题解 | #字符统计#

字符统计

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

全部评论

相关推荐

牛客928043833号:在他心里你已经是他的员工了
点赞 评论 收藏
分享
05-01 22:41
中南大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务