题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.*;
// 解法思路差不多,都是记录累积字符出现的次数,然后计算加权
// 下面是我21分钟的解法,然而我看到解题区有亮点的解法:是如何记录累积字符次数呢?用对应128的ASCII数组
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
in.nextLine();
while (in.hasNextLine()) {
String a = in.nextLine() ;
List<Integer> list = new ArrayList<>();
char[] chars = a.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (a.indexOf(String.valueOf(chars[i])) == i) {
int length = a.replace(String.valueOf(chars[i]), "").length();
int num = a.length() - length;
list.add(num);
}
}
int max = 0;
int n = 26;
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
max = max + next*n;
n--;
}
System.out.println(max);
}
}
}
