题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.Scanner; import java.util.HashMap; import java.util.Map; import java.util.Arrays; // 注意类名必须为 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 a = in.nextInt(); for (int i = 0; i < a; i++) { String s1 = in.next(); System.out.println(getMaxBeauty(s1)); } } } private static int getMaxBeauty(String str) { Map<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < str.length(); i++) { String c = str.charAt(i) + ""; map.put(c,map.getOrDefault(c,0)+1); } int [] v = new int[map.size()]; int l = 0; for (String s : map.keySet()) { v[l] = map.get(s); l++; } Arrays.sort(v); int sum = 0; for (int j = v.length-1, m = 26; j >= 0; j--) { sum = sum + m * v[j]; m--; } return sum; } }
}