题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.*; import java.util.Map.Entry; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static int calculate(String name) { Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); map.merge(ch, 1, (oldVal, newVal) -> oldVal + 1); } Map<Character, Integer> treeMap = new TreeMap<>((c1, c2) -> { if (map.get(c1).equals(map.get(c2))) { return c1 - c2; } return map.get(c2) - map.get(c1); }); treeMap.putAll(map); int ans = 0; int point = 26; for (Entry<Character, Integer> entry : treeMap.entrySet()) { Integer quantity = entry.getValue(); ans += point * quantity; point -= 1; } return ans; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { String name = sc.next(); int score = calculate(name); System.out.println(score); } } }