题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.*; import java.util.stream.Collectors; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); in.nextLine(); for (int i = 0; i < a; i++) { String line = in.nextLine(); System.out.println(getVal(line)); } } public static int getVal(String str) { str = str.toLowerCase(); int size = 0; Map<Character, Integer> map = new TreeMap<Character, Integer>(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); map.putIfAbsent(c, 0); map.put(c, map.get(c) + 1); } int flag = 26; List<Integer> list = map.values().stream().sorted().collect(Collectors.toList()); for (int index = list.size() - 1; index >= 0; index--,flag--) { size += list.get(index) * flag; } return size; } }