题解 | #名字的漂亮度#
名字的漂亮度
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 sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { String s = sc.next(); HashMap<Character, Integer> map = new HashMap<>(); for (int j = 0; j < s.length(); j++) { map.put(s.charAt(j), map.getOrDefault(s.charAt(j), 0) + 1); } List<Map.Entry<Character, Integer>> list = map.entrySet().stream() .sorted(Map.Entry.<Character, Integer>comparingByValue() .reversed()).collect(Collectors.toList()); int res = 0; int begin = 26; for (Map.Entry<Character, Integer> entry : list) { res += begin * entry.getValue(); begin--; //System.out.println(entry.getKey() + " " + entry.getValue()); } System.out.println(res); } } }