Skip to content

Commit 2a6f152

Browse files
[LEET-451] add 451
1 parent 2bbbd5a commit 2a6f152

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
|455|[Assign Cookies](https://leetcode.com/problems/assign-cookies/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/AssignCookies.java)| O(n)|O(1) | Easy|
2525
|454|[4Sum II](https://leetcode.com/problems/4sum-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/_4SumII.java) | O(n) |O(n) | Medium| HashMap
2626
|453|[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MinimumMovestoEqualArrayElements.java)| O(n)|O(1) | Easy|
27+
|451|[Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SortCharactersByFrequency.java) | O(nlogn) |O(n) | Medium| HashMap
2728
|448|[Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindAllNumbersDisappearedinanArray.java)| O(n)|O(1) | Easy| Array, HashMap
2829
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/NumberofBoomerangs.java)| O(n^2)|O(n) | Easy| HashMap
2930
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindAllDuplicatesinanArray.java)| O(n)|O(1) | Medium| Array
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Given a string, sort it in decreasing order based on the frequency of characters.
7+
8+
Example 1:
9+
10+
Input:
11+
"tree"
12+
13+
Output:
14+
"eert"
15+
16+
Explanation:
17+
'e' appears twice while 'r' and 't' both appear once.
18+
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
19+
20+
Example 2:
21+
22+
Input:
23+
"cccaaa"
24+
25+
Output:
26+
"cccaaa"
27+
28+
Explanation:
29+
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
30+
Note that "cacaca" is incorrect, as the same characters must be together.
31+
32+
Example 3:
33+
34+
Input:
35+
"Aabb"
36+
37+
Output:
38+
"bbAa"
39+
40+
Explanation:
41+
"bbaA" is also a valid answer, but "Aabb" is incorrect.
42+
Note that 'A' and 'a' are treated as two different characters.
43+
44+
*/
45+
public class SortCharactersByFrequency {
46+
47+
public String frequencySort(String s) {
48+
Map<Character, Integer> map = new HashMap();
49+
for (char c : s.toCharArray()) map.put(c, map.getOrDefault(c, 0) + 1);
50+
List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
51+
Collections.sort(list, (o1, o2) -> (o2.getValue()).compareTo(o1.getValue()));
52+
StringBuilder stringBuilder = new StringBuilder();
53+
for (Map.Entry<Character, Integer> entry : list) {
54+
for (int i = 0; i < entry.getValue(); i++) {
55+
stringBuilder.append(entry.getKey());
56+
}
57+
}
58+
return stringBuilder.toString();
59+
}
60+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions.SortCharactersByFrequency;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static junit.framework.Assert.assertEquals;
9+
10+
/**
11+
* Created by stevesun on 1/15/17.
12+
*/
13+
public class SortCharactersByFrequencyTest {
14+
private static SortCharactersByFrequency test;
15+
private static String expected;
16+
private static String actual;
17+
private static String input;
18+
19+
@BeforeClass
20+
public static void setup(){
21+
test = new SortCharactersByFrequency();
22+
}
23+
24+
@Before
25+
public void setupForEachTest(){
26+
expected = "";
27+
actual = "";
28+
}
29+
30+
@Test
31+
public void test1(){
32+
input = "tree";
33+
expected = "eert";
34+
actual = test.frequencySort(input);
35+
assertEquals(expected, actual);
36+
}
37+
38+
@Test
39+
public void test2(){
40+
input = "cccaaa";
41+
expected = "aaaccc";
42+
actual = test.frequencySort(input);
43+
assertEquals(expected, actual);
44+
}
45+
46+
@Test
47+
public void test3(){
48+
input = "Aabb";
49+
expected = "bbAa";
50+
actual = test.frequencySort(input);
51+
assertEquals(expected, actual);
52+
}
53+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy