题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
List<String> arr = new ArrayList<>();
List<Integer> out = new ArrayList<>();
for (int i = 0; i < n; i++) {
arr.add(sc.next());
}
arr.forEach(i -> out.add(court(i)));
out.forEach(System.out::println);
}
sc.close();
}
private static Integer court(String i) {
int court = 0;
HashMap<Character, Integer> charCount = new HashMap<>();
for (char c : i.toCharArray()) {
if (charCount.containsKey(c)) {
charCount.put(c, charCount.get(c) + 1);
} else {
charCount.put(c, 1);
}
}
List<Integer> sortedCharCount = new ArrayList<>();
charCount.forEach((key, value) -> sortedCharCount.add(value));
//排序
Collections.sort(sortedCharCount);
//反转
Collections.reverse(sortedCharCount);
int y = 26;
// 将每个字符的计数乘以26
for (Integer integers : sortedCharCount) {
court += integers * y;
y--;
}
return court;
}
}
