Skip to content

Commit 55ab10b

Browse files
committed
451. Sort Characters By Frequency
1 parent f167400 commit 55ab10b

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ The project is divided into two parts: `structure` and `solution`.
257257

258258
| No. | Title | Difficulty | Solution | Idea |
259259
| ---- | ------------------------------------------------------------ | ---------- | ------------------------------------------------------------ | ---------------------------------------- |
260+
| 451 | [Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/) | Medium | [SortCharactersByFrequency.java](src/leetcode/solution/Hash/SortCharactersByFrequency.java) | Map and Sort. Or Bucket Sort. |
260261
| 567 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) | Medium | [LongestConsecutiveSequence.java](src/leetcode/solution/Hash/LongestConsecutiveSequence.java) | Hash Table: using HashSet save the nums. |
261262

262263
#### [Graph](src/leetcode/solution/Graph)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package leetcode.solution.Hash;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* 451. Sort Characters By Frequency
10+
*/
11+
public class SortCharactersByFrequency {
12+
13+
14+
public static void main(String[] args) {
15+
String s = "wqaqaaasddd";
16+
SortSolution sortSolution = new SortSolution();
17+
String ans = sortSolution.frequencySort(s);
18+
System.out.println(ans);
19+
// aaaadddqqsw
20+
21+
BucketSolution bucketSolution = new BucketSolution();
22+
ans = bucketSolution.frequencySort(s);
23+
System.out.println(ans);
24+
}
25+
26+
}
27+
28+
class SortSolution {
29+
30+
31+
public String frequencySort(String s) {
32+
Map<Character, Integer> count = new HashMap<>();
33+
char[] chars = s.toCharArray();
34+
35+
for (char c : chars) {
36+
count.put(c, count.getOrDefault(c, 0) + 1);
37+
}
38+
39+
List<Character> list = new ArrayList<>(count.keySet());
40+
41+
list.sort((a, b) -> {
42+
return count.get(b) - count.get(a);
43+
});
44+
45+
StringBuilder sb = new StringBuilder();
46+
47+
for (char c : list) {
48+
int num = count.get(c);
49+
for (int i = 0; i < num; i++) {
50+
sb.append(c);
51+
}
52+
}
53+
54+
return sb.toString();
55+
56+
}
57+
}
58+
59+
60+
class BucketSolution {
61+
62+
63+
public String frequencySort(String s) {
64+
Map<Character, Integer> count = new HashMap<>();
65+
char[] chars = s.toCharArray();
66+
67+
for (char c : chars) {
68+
count.put(c, count.getOrDefault(c, 0) + 1);
69+
}
70+
71+
int max = 0;
72+
for (Integer v : count.values()) {
73+
max = Math.max(max, v);
74+
}
75+
76+
List<List<Character>> buckets = new ArrayList<>();
77+
78+
for (int i = 0; i < max + 1; i++) {
79+
buckets.add(new ArrayList<>());
80+
}
81+
82+
for (Map.Entry<Character, Integer> entry : count.entrySet()) {
83+
buckets.get(entry.getValue()).add(entry.getKey());
84+
}
85+
86+
StringBuilder sb = new StringBuilder();
87+
88+
for (int i = buckets.size() - 1; i >= 0; i--) {
89+
List<Character> list = buckets.get(i);
90+
if (!list.isEmpty()) {
91+
for (Character c : list) {
92+
for (int j = 0; j < i; j++) {
93+
sb.append(c);
94+
}
95+
}
96+
}
97+
}
98+
99+
return sb.toString();
100+
101+
}
102+
}

0 commit comments

Comments
 (0)