题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(sc.next());
}
for (int i = 0; i < list.size(); i++) {
function(list.get(i));
}
}
public static void function(String str) {
HashMap<String, Integer> hm = new HashMap<>();
for (String s : str.split("")) {
hm.put(s, hm.getOrDefault(s, 0) + 1);
}
ArrayList<Integer> F = new ArrayList<>();
for (String s : hm.keySet()) {
F.add(hm.get(s));
}
F.sort((Integer o1, Integer o2) -> {return o2 - o1;});
int result = 0;
int mul = 26;
for (int j = 0; j <= F.size() - 1; j++) {
result += F.get(j) * mul;
mul--;
}
System.out.println(result);
}
}



查看1道真题和解析