题解 | 名字的漂亮度

名字的漂亮度

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while (T != 0) {
            T--;
            String str = in.next();
            PriorityQueue<Integer> priorityQueue = new PriorityQueue<>
            (new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2 - o1;
                }
            });
            Map<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (!map.containsKey(c)) {
                    map.put(c, 1);
                } else {
                    map.put(c, map.get(c) + 1);
                }
            }
            Iterator<Character> iterator = map.keySet().iterator();
            while (iterator.hasNext()) {
                Character next = iterator.next();
                Integer value = map.get(next);
                priorityQueue.add(value);
            }
            int bDegree = 26;
            int res = 0;
            while (!priorityQueue.isEmpty()) {
                Integer poll = priorityQueue.poll();
                res += poll * bDegree;
                bDegree--;
            }
            System.out.println(res);
        }
    }
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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