题解 | #名字的漂亮度#

名字的漂亮度

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

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

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int N = in.nextInt();
            for (int i = 0; i < N; i++) {
                System.out.println(maxBeauty(in.next()));
            }
        }
    }

    static int maxBeauty(String str) {
        Map<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < str.length(); i++) {
            map.put((int) str.charAt(i), map.getOrDefault((int)str.charAt(i), 0) + 1);
        }
        List<Map.Entry<Integer, Integer>> list = map.entrySet().stream()
                .sorted((x, y) -> y.getValue() - x.getValue())
                .collect(Collectors.toList());
        int beauty = 0, i = 26;
        for (Map.Entry<Integer, Integer> kv : list) {
            beauty += kv.getValue() * i--;
        }
        return beauty;
    }
}


全部评论

相关推荐

牛客78682892...:直接点还好,总比要了简历也不回的强
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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