名字的漂亮度

名字的漂亮度

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;

    }
}
全部评论

相关推荐

06-03 15:32
点赞 评论 收藏
分享
04-14 20:10
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
吴offer选手:HR:我KPI到手了就行,合不合适关我什么事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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