题目
题源
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/63dd17ecf28aba84a3ed6728d9e14d44.png)
代码
class Solution {
public String frequencySort(String s) {
Map<Character,Integer> hashMap = new HashMap<>();
char[] s2c = s.toCharArray();
for (int i = 0; i < s2c.length; i++) {
hashMap.put(s2c[i], hashMap.getOrDefault(s2c[i], 0) + 1);
}
List<Character>[] bucket = new ArrayList[s2c.length + 1];
for (char c : hashMap.keySet()) {
int val = hashMap.get(c);
if (bucket[val] == null) {
bucket[val] = new ArrayList<>();
}
bucket[val].add(c);
}
StringBuffer sb = new StringBuffer();
for (int i = s2c.length; i >= 0; i--) {
if (bucket[i] == null) continue;
for (char c : bucket[i]) {
for (int j = 0; j < i; j++) {
sb.append(c);
}
}
}
return sb.toString();
}
}