题解 | #名字的漂亮度#

名字的漂亮度

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

让出现次数越多的字符,权重越大即可。

import java.util.Scanner;
import java.util.Arrays;
public class Main { 
    private final int N = 26;
    
//     public Main() {
        
//     }

    public int count(String name) {
        int[] arr = new int[N];
        for (int i = 0; i < name.length(); i++) {
            char ch = name.charAt(i);
            arr[ch - 'a']++;
        }
        Arrays.sort(arr);
        int result = 0;
        for (int weight = N, i = N - 1; i >= 0; i--) {
            if (arr[i] != 0) {
                result += weight * arr[i];
                weight--;
            }
        }
        return result;
    }

    public static void main(String[] args) 
    {
        Main solution = new Main();
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            int n = Integer.parseInt(in.nextLine());
            for (int i = 0; i < n; i++) {
                String name = in.nextLine().toLowerCase();
                int result = solution.count(name);
                System.out.println(result);
            }
        }
    } 
}  


全部评论

相关推荐

不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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