题解 | #名字的漂亮度#

名字的漂亮度

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        String[] strs = new String[N];
        for(int i=0;i<N;i++){
            strs[i]=sc.next();
        }
        int[] result = new int[N];
        for (int i = 0; i < N; i++) {
            Map<Character,Integer> map = new HashMap<>();
            String str = strs[i];
            for (int j = 0; j < str.length(); j++) {
                if(map.get(str.charAt(j))==null){
                    map.put(str.charAt(j),1);
                }
                else{
                    map.put(str.charAt(j),map.get(str.charAt(j))+1);
                }
            }
            List<Map.Entry<Character,Integer>> list = new ArrayList<>(map.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
                @Override
                public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
                    return o2.getValue().compareTo(o1.getValue());
                }
            });
            int beauty = 26;
            int res = 0;
            for (Map.Entry<Character,Integer> entry:list) {
                res  += entry.getValue()*(beauty--);
            }
			System.out.println(res);
        }
    }
}

全部评论

相关推荐

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