451. Sort Characters By Frequency
難易度
Medium
アプローチ
Hashmap
class Solution {
public String frequencySort(String s) {
HashMap<Character, Integer> hashMap = new HashMap<>();
for (char c : s.toCharArray()) {
hashMap.put(c, hashMap.getOrDefault(c, 0) + 1);
}
return hashMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue((o1, o2) -> -o1.compareTo(o2)))
.map(entry -> String.valueOf(entry.getKey()).repeat(entry.getValue()))
.map(String::valueOf)
.collect(Collectors.joining());
/*
return hashMap.entrySet().stream()
.sorted((o1, o2) -> o2.getValue() - o1.getValue())
.map(obj -> String.valueOf(obj.getKey()).repeat(obj.getValue()))
.map(String::valueOf)
.collect(Collectors.joining());
*/
}
}
- こちらの回答を参照しました