题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int i1 = scanner.nextInt(); ArrayList<String> strings = new ArrayList<>(); for (int i = 0; i <i1; i++) { String s = scanner.next(); strings.add(s); } for (int i = 0; i <i1; i++) { System.out.println(reback(strings.get(i))); } } } public static Integer reback(String s){ String[] split = s.split(""); HashMap<String, Integer> hs = new HashMap<>(); for (int i = 0; i < split.length; i++) { if (hs.get(split[i])!=null){ hs.put(split[i],hs.get(split[i])+1); }else { hs.put(split[i],1); } } ArrayList<Integer> it = new ArrayList<>(); Iterator<Map.Entry<String, Integer>> ith = hs.entrySet().iterator(); while (ith.hasNext()){ Map.Entry<String, Integer> next = ith.next(); Integer value = next.getValue(); it.add(value); } Collections.sort(it); int count=26; int sum=0; for (int i = it.size()-1; i >=0; i--) { sum=sum+count*it.get(i); count--; } return sum; } }