名字的漂亮度

名字的漂亮度

http://www.nowcoder.com/questionTerminal/02cb8d3597cf416d9f6ae1b9ddc4fde3

使用计数排序即可

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {
         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n;
        String str;
        while ((str = reader.readLine()) != null) {
            n = Integer.parseInt(str);
            for (int i = 0; i < n; i++) {
                str = reader.readLine();
                System.out.println(solution(str));
            }
        }
        reader.close();
    }

    private static int solution(String str) {
        int[] count = new int[26];
        str = str.toLowerCase();
        for (int i = 0; i < str.length(); i++) {
            count[str.charAt(i) - 'a']++;
        }
        Arrays.sort(count);
        int res = 0;
        for (int i = count.length - 1, max = 26; i >= 0; i--) {
            if (count[i] == 0) break;
            res += count[i] * max;
            max--;
        }
        return  res;

    }
}
全部评论

相关推荐

07-10 12:17
已编辑
商丘师范学院 Java
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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