题解 | #名字的漂亮度#
名字的漂亮度
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.hasNext()) { // 注意 while 处理多个 case
int n = in.nextInt();
String[] strs = new String[n];
for(int i=0;i<n;i++){
strs[i] = in.next();
}
for(int i=0;i<n;i++){
System.out.println(getStrLevel(strs[i]));
}
}
}
private static int getStrLevel(String str) {
int[] charCount = new int[26];
char[] chars = str.toLowerCase().toCharArray();
for (int i = 0; i < chars.length; i++) {
int charIndex = chars[i] - 'a';
charCount[charIndex] += 1;
}
// 字符个数排序
Arrays.sort(charCount);
int res = 0;
int beautyLevel = 26;
for (int i = charCount.length - 1; i >= 0; i--) {
if (charCount[i] != 0) {
res += charCount[i] * beautyLevel;
beautyLevel--;
}
}
return res;
}
}
传音控股公司福利 359人发布
