题解 | #名字的漂亮度#

名字的漂亮度

https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3

import java.util.*;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            String s = sc.next();
            HashMap<Character, Integer> map = new HashMap<>();
            for (int j = 0; j < s.length(); j++) {
                map.put(s.charAt(j), map.getOrDefault(s.charAt(j), 0) + 1);
            }
            List<Map.Entry<Character, Integer>> list = map.entrySet().stream()
                    .sorted(Map.Entry.<Character, Integer>comparingByValue()
                    .reversed()).collect(Collectors.toList());
            int res = 0;
            int begin = 26;
            for (Map.Entry<Character, Integer> entry : list) {
                res += begin * entry.getValue();
                begin--;
                //System.out.println(entry.getKey() + " " + entry.getValue());
            }
            System.out.println(res);
        }
    }
}

全部评论

相关推荐

notbeentak...:孩子,说实话,选择很重要,可能你换一个方向会好很多,但是现在时间不太够了,除非准备春招
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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