题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
拿到每个字母的个数,然后倒序排序,从前到后分别乘以26,25,24.....
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();
for(int i=0;i<N;i++){
getBeauty(in.next());
}
}
}
public static void getBeauty(String str){
Map<String, Integer> map = new HashMap<>();
for(int i=0;i<str.length();i++){
String s = str.substring(i,i+1);
if(map.keySet().contains(s)){
map.put(s, map.get(s)+1);
}else{
map.put(s, 1);
}
}
ArrayList<Integer> list = new ArrayList<>();
for(String s:map.keySet()){
list.add(map.get(s));
}
// 倒序排
list.sort((o1, o2) -> o2 - o1);
int count = 0;
int firstLength = 26;
for(int i=0;i<list.size();i++){
count = count + list.get(i) * firstLength;
firstLength--;
}
System.out.println(count);
}
}
