题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
for (int i = 0; i < a; i++) {
char[] chars = in.next().toCharArray();
//统计字符个数 定义一个26长度的数组
int[] count = new int[26];
for (int j = 0; j < chars.length; j++) {
count[chars[j] - 'a'] += 1;
}
Arrays.sort(count);
//输出结果
int sum = 0;
for (int j = count.length - 1; j >= 0; j--) {
sum += count[j] * (j + 1);//(j+1) 等于26 25 24
}
System.out.println(sum);
}
}
}
}