Skip to content

Commit 9c79a76

Browse files
add a solution for 767
1 parent e67e2f1 commit 9c79a76

File tree

2 files changed

+75
-26
lines changed

2 files changed

+75
-26
lines changed

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

+43
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,47 @@ public CustChar(Character c, int count) {
5858
}
5959
}
6060
}
61+
62+
public static class Solution2 {
63+
/**
64+
* My completely original solution on 12/24/2021.
65+
*/
66+
public String reorganizeString(String s) {
67+
Map<Character, Integer> map = new HashMap<>();
68+
for (char c : s.toCharArray()) {
69+
map.put(c, map.getOrDefault(c, 0) + 1);
70+
}
71+
PriorityQueue<Tuple> maxHeap = new PriorityQueue<>((a, b) -> b.count - a.count);
72+
for (char c : map.keySet()) {
73+
maxHeap.add(new Tuple(c, map.get(c)));
74+
}
75+
StringBuilder sb = new StringBuilder("1");
76+
while (!maxHeap.isEmpty()) {
77+
PriorityQueue<Tuple> tmp = new PriorityQueue<>((a, b) -> b.count - a.count);
78+
Tuple curr = maxHeap.poll();
79+
while (sb.length() != 0 && sb.charAt(sb.length() - 1) == curr.c && !maxHeap.isEmpty()) {
80+
tmp.offer(curr);
81+
curr = maxHeap.poll();
82+
}
83+
if (curr.c != sb.charAt(sb.length() - 1)) {
84+
sb.append(curr.c);
85+
}
86+
maxHeap.addAll(tmp);
87+
if (curr.count > 1) {
88+
maxHeap.offer(new Tuple(curr.c, curr.count - 1));
89+
}
90+
}
91+
return sb.substring(1).length() != s.length() ? "" : sb.substring(1);
92+
}
93+
94+
class Tuple {
95+
char c;
96+
int count;
97+
98+
public Tuple(char c, int count) {
99+
this.c = c;
100+
this.count = count;
101+
}
102+
}
103+
}
61104
}

src/test/java/com/fishercoder/_767Test.java

+32-26
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,36 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _767Test {
10-
private static _767.Solution1 solution1;
11-
12-
@BeforeClass
13-
public static void setup() {
14-
solution1 = new _767.Solution1();
15-
}
16-
17-
@Test
18-
public void test1() {
19-
assertEquals("aba", solution1.reorganizeString("aab"));
20-
}
21-
22-
@Test
23-
public void test2() {
24-
assertEquals("", solution1.reorganizeString("aaab"));
25-
}
26-
27-
@Test
28-
public void test3() {
29-
assertEquals("bababab", solution1.reorganizeString("aaabbbb"));
30-
}
31-
32-
@Test
33-
public void test4() {
34-
assertEquals("vovlv", solution1.reorganizeString("vvvlo"));
35-
}
10+
private static _767.Solution1 solution1;
11+
private static _767.Solution2 solution2;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _767.Solution1();
16+
solution2 = new _767.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals("aba", solution1.reorganizeString("aab"));
22+
assertEquals("aba", solution2.reorganizeString("aab"));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals("", solution1.reorganizeString("aaab"));
28+
assertEquals("", solution2.reorganizeString("aaab"));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals("bababab", solution1.reorganizeString("aaabbbb"));
34+
assertEquals("bababab", solution2.reorganizeString("aaabbbb"));
35+
}
36+
37+
@Test
38+
public void test4() {
39+
assertEquals("vovlv", solution1.reorganizeString("vvvlo"));
40+
assertEquals("vovlv", solution2.reorganizeString("vvvlo"));
41+
}
3642
}

0 commit comments

Comments
 (0)