Skip to content

Commit c850dc9

Browse files
add a solution for 451
1 parent 31aa665 commit c850dc9

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/main/java/com/fishercoder/solutions/_451.java

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.HashMap;
66
import java.util.List;
77
import java.util.Map;
8+
import java.util.TreeMap;
89

910
public class _451 {
1011

@@ -25,4 +26,31 @@ public String frequencySort(String s) {
2526
return stringBuilder.toString();
2627
}
2728
}
29+
30+
public static class Solution2 {
31+
public String frequencySort(String s) {
32+
Map<Character, Integer> map = new HashMap<>();
33+
for (char c : s.toCharArray()) {
34+
map.put(c, map.getOrDefault(c, 0) + 1);
35+
}
36+
TreeMap<Integer, List<Character>> reverseMap = new TreeMap<>(Collections.reverseOrder());
37+
for (char c : map.keySet()) {
38+
int freq = map.get(c);
39+
if (!reverseMap.containsKey(freq)) {
40+
reverseMap.put(freq, new ArrayList<>());
41+
}
42+
reverseMap.get(freq).add(c);
43+
}
44+
StringBuilder sb = new StringBuilder();
45+
for (int freq : reverseMap.keySet()) {
46+
List<Character> list = reverseMap.get(freq);
47+
for (char c : list) {
48+
for (int i = 0; i < freq; i++) {
49+
sb.append(c);
50+
}
51+
}
52+
}
53+
return sb.toString();
54+
}
55+
}
2856
}
+8-17
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,44 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._451;
4-
import org.junit.Before;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
import static junit.framework.Assert.assertEquals;
98

10-
/**
11-
* Created by fishercoder on 1/15/17.
12-
*/
139
public class _451Test {
1410
private static _451.Solution1 solution1;
11+
private static _451.Solution2 solution2;
1512
private static String expected;
16-
private static String actual;
1713
private static String input;
1814

1915
@BeforeClass
2016
public static void setup() {
2117
solution1 = new _451.Solution1();
22-
}
23-
24-
@Before
25-
public void setupForEachTest() {
26-
expected = "";
27-
actual = "";
18+
solution2 = new _451.Solution2();
2819
}
2920

3021
@Test
3122
public void test1() {
3223
input = "tree";
3324
expected = "eert";
34-
actual = solution1.frequencySort(input);
35-
assertEquals(expected, actual);
25+
assertEquals(expected, solution1.frequencySort(input));
26+
assertEquals(expected, solution2.frequencySort(input));
3627
}
3728

3829
@Test
3930
public void test2() {
4031
input = "cccaaa";
4132
expected = "aaaccc";
42-
actual = solution1.frequencySort(input);
43-
assertEquals(expected, actual);
33+
assertEquals(expected, solution1.frequencySort(input));
34+
assertEquals(expected, solution2.frequencySort(input));
4435
}
4536

4637
@Test
4738
public void test3() {
4839
input = "Aabb";
4940
expected = "bbAa";
50-
actual = solution1.frequencySort(input);
51-
assertEquals(expected, actual);
41+
assertEquals(expected, solution1.frequencySort(input));
42+
assertEquals(expected, solution2.frequencySort(input));
5243
}
5344
}

0 commit comments

Comments
 (0)