Skip to content

Commit 0e10296

Browse files
add 767
1 parent a108882 commit 0e10296

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|767|[Reorganize String](https://leetcode.com/problems/reorganize-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_767.java) | O(klogk) k is the number of unique characters in given String| O(k) | |Medium|
2526
|766|[Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_766.java) | O(m*n) | O(1) | |Easy|
2627
|765|[Couples Holding Hands](https://leetcode.com/problems/couples-holding-hands/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_765.java) | O(n^2) | O(1) | |Hard|
2728
|764|[Largest Plus Sign](https://leetcode.com/problems/largest-plus-sign/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_764.java) | O(n^2) | O(n^2) | |Medium| DP
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.PriorityQueue;
6+
7+
/**
8+
* 767. Reorganize String
9+
*
10+
* Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.
11+
12+
If possible, output any possible result. If not possible, return the empty string.
13+
14+
Example 1:
15+
16+
Input: S = "aab"
17+
Output: "aba"
18+
19+
Example 2:
20+
21+
Input: S = "aaab"
22+
Output: ""
23+
24+
Note:
25+
26+
S will consist of lowercase letters and have length in range [1, 500].
27+
*/
28+
29+
public class _767 {
30+
public static class Solution1 {
31+
public String reorganizeString(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+
int len = S.length();
37+
for (char c : map.keySet()) {
38+
if ((len % 2 == 0 && map.get(c) > len / 2) || (len % 2 != 0 && map.get(c) >= len / 2 + 2)) {
39+
return "";
40+
}
41+
}
42+
PriorityQueue<CustChar> queue = new PriorityQueue<>((a, b) -> b.count - a.count);
43+
for (char c : map.keySet()) {
44+
queue.offer(new CustChar(c, map.get(c)));
45+
}
46+
47+
StringBuilder sb = new StringBuilder();
48+
while (!queue.isEmpty()) {
49+
CustChar curr = queue.poll();
50+
char c = curr.c;
51+
if (sb.length() > 0 && sb.charAt(sb.length() - 1) != c) {
52+
sb.append(c);
53+
if (curr.count > 1) {
54+
queue.offer(new CustChar(c, curr.count - 1));
55+
}
56+
} else if (sb.length() == 0) {
57+
sb.append(c);
58+
if (curr.count > 1) {
59+
queue.offer(new CustChar(c, curr.count - 1));
60+
}
61+
} else if (sb.length() > 0 && sb.charAt(sb.length() - 1) == c && !queue.isEmpty()) {
62+
CustChar next = queue.poll();
63+
sb.append(next.c);
64+
if (next.count > 1) {
65+
queue.offer(new CustChar(next.c, next.count - 1));
66+
}
67+
queue.offer(curr);
68+
}
69+
}
70+
return sb.toString();
71+
}
72+
73+
class CustChar {
74+
Character c;
75+
int count;
76+
77+
public CustChar(Character c, int count) {
78+
this.c = c;
79+
this.count = count;
80+
}
81+
}
82+
}
83+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._767;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
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+
}
36+
}

0 commit comments

Comments
 (0)