Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc850dc9

Browse files
add a solution for 451
1 parent31aa665 commitc850dc9

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
importjava.util.HashMap;
66
importjava.util.List;
77
importjava.util.Map;
8+
importjava.util.TreeMap;
89

910
publicclass_451 {
1011

@@ -25,4 +26,31 @@ public String frequencySort(String s) {
2526
returnstringBuilder.toString();
2627
}
2728
}
29+
30+
publicstaticclassSolution2 {
31+
publicStringfrequencySort(Strings) {
32+
Map<Character,Integer>map =newHashMap<>();
33+
for (charc :s.toCharArray()) {
34+
map.put(c,map.getOrDefault(c,0) +1);
35+
}
36+
TreeMap<Integer,List<Character>>reverseMap =newTreeMap<>(Collections.reverseOrder());
37+
for (charc :map.keySet()) {
38+
intfreq =map.get(c);
39+
if (!reverseMap.containsKey(freq)) {
40+
reverseMap.put(freq,newArrayList<>());
41+
}
42+
reverseMap.get(freq).add(c);
43+
}
44+
StringBuildersb =newStringBuilder();
45+
for (intfreq :reverseMap.keySet()) {
46+
List<Character>list =reverseMap.get(freq);
47+
for (charc :list) {
48+
for (inti =0;i <freq;i++) {
49+
sb.append(c);
50+
}
51+
}
52+
}
53+
returnsb.toString();
54+
}
55+
}
2856
}
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,44 @@
11
packagecom.fishercoder;
22

33
importcom.fishercoder.solutions._451;
4-
importorg.junit.Before;
54
importorg.junit.BeforeClass;
65
importorg.junit.Test;
76

87
importstaticjunit.framework.Assert.assertEquals;
98

10-
/**
11-
* Created by fishercoder on 1/15/17.
12-
*/
139
publicclass_451Test {
1410
privatestatic_451.Solution1solution1;
11+
privatestatic_451.Solution2solution2;
1512
privatestaticStringexpected;
16-
privatestaticStringactual;
1713
privatestaticStringinput;
1814

1915
@BeforeClass
2016
publicstaticvoidsetup() {
2117
solution1 =new_451.Solution1();
22-
}
23-
24-
@Before
25-
publicvoidsetupForEachTest() {
26-
expected ="";
27-
actual ="";
18+
solution2 =new_451.Solution2();
2819
}
2920

3021
@Test
3122
publicvoidtest1() {
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
publicvoidtest2() {
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
publicvoidtest3() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp