题解 | #名字的漂亮度#函数式解法
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.Arrays; import java.util.Comparator; import java.util.Map; import java.util.Scanner; import java.util.function.Function; import java.util.stream.Collectors; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = Integer.parseInt(in.nextLine()); for (int i = 0; i < n; i++) { Map<String, Long> map = Arrays.stream(in.nextLine().split("")).collect( Collectors.groupingBy(Function.identity(), Collectors.counting())); int beauty = 26; int sum = 0; for (Long num : map.values().stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList())) { sum += beauty-- * num; } System.out.println(sum); } } } }