题目:
题解:
贪心策略:要获得最大的漂亮度,优先将较大漂亮度分给出现次数最多的字符。
public int getBeautifulCount(String string) {
Map<Character, Integer> map = new HashMap<>(string.length());
for (char c : string.toCharArray()) {
char temp = Character.toLowerCase(c);
map.put(temp, map.getOrDefault(temp, 0)+1);
}
List<Integer> valueList = map.values().stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
int result = 0, step = 26;
for (Integer value : valueList) {
result += value * step;
step--;
}
return result;
}
时间复杂度:O(n)