-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortCharactersByFrequency.java
42 lines (32 loc) · 1.33 KB
/
SortCharactersByFrequency.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.*;
public class SortCharactersByFrequency {
public String frequencySort(String s) {
HashMap<Character, Integer> hashMap = new HashMap<>();
for (char c : s.toCharArray()) {
if (hashMap.containsKey(c)) {
hashMap.replace(c, hashMap.get(c) + 1);
} else {
hashMap.put(c, 1);
}
}
List<Map.Entry<Character, Integer>> entryList = new ArrayList<>(hashMap.entrySet());
// Sort the list by values in ascending order
entryList.sort(Map.Entry.comparingByValue());
// Create a new LinkedHashMap to store the sorted entries
LinkedHashMap<Character, Integer> sortedMap = new LinkedHashMap<>();
for (Map.Entry<Character, Integer> entry : entryList) {
sortedMap.put(entry.getKey(), entry.getValue());
}
StringBuffer sc = new StringBuffer();
sortedMap.forEach((character, integer) -> {
for (int i = 0; i < integer; i++) {
sc.append(character);
}
});
return sc.reverse().toString();
}
public static void main(String[] args) {
SortCharactersByFrequency sortCharactersByFrequency = new SortCharactersByFrequency();
System.out.println(sortCharactersByFrequency.frequencySort("tree"));
}
}