题解 | #名字的漂亮度#

名字的漂亮度

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 in = new Scanner(System.in);
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            int N = Integer.parseInt(in.nextLine());
            for (int i = 0; i < N; i++) {
                String str = in.nextLine();
                int result = beautyLen(str);
                System.out.println(result);
            }
        }
    }

    private static int beautyLen(String str) {
        char[] chars = str.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();


        for (char ch : chars) {
            Integer count = map.getOrDefault(ch, 0);
            map.put(ch, count + 1);
        }

        List<Integer> list = map.values().stream().sorted(
                                 Comparator.reverseOrder()).collect(Collectors.toList());

        int beautyLen = 0;
        int beauty = 26;

        for (Integer count : list) {
            beautyLen += count * beauty;
            beauty--;
        }

        return beautyLen;
    }
}

#刷题#
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务