Skip to content

Commit bdc2811

Browse files
add 358
1 parent 6570beb commit bdc2811

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Your ideas/fixes/algorithms are more than welcome!
196196
|361|[Bomb Enemy](https://leetcode.com/problems/bomb-enemy/)|[Solution](../master/src/main/java/com/fishercoder/solutions/BombEnemies.java)| O(?)|O(?) | Medium|
197197
|360|[Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_360.java)| O(n)|O(1) | Medium| Two Pointers, Math
198198
|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/LoggerRateLimiter.java)| amortized O(1)|O(k) | Easy| HashMap
199+
|358|[Rearrange String k Distance Apart](https://leetcode.com/problems/rearrange-string-k-distance-apart/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_358.java)| O(n)|O(n) | Hard| HashMap, Heap, Greedy
199200
|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/CountNumbersWithUniqueDigits.java)| O(?)|O(?) | Medium|
200201
|356|[Line Reflection](https://leetcode.com/problems/line-reflection/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_356.java)| O(n)|O(n) | Medium| HashSet
201202
|355|[Design Twitter](https://leetcode.com/problems/design-twitter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_355.java)| O(n)|O(n) | Medium| Design, HashMap, Heap
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 358. Rearrange String k Distance Apart
7+
*
8+
* Given a non-empty string s and an integer k, rearrange the string such that the same characters are at least distance k from each other.
9+
10+
All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string "".
11+
Example 1:
12+
s = "aabbcc", k = 3
13+
Result: "abcabc"
14+
The same letters are at least distance 3 from each other.
15+
16+
Example 2:
17+
s = "aaabc", k = 3
18+
Answer: ""
19+
It is not possible to rearrange the string.
20+
21+
Example 3:
22+
s = "aaadbbcc", k = 2
23+
Answer: "abacabcd"
24+
Another possible answer is: "abcabcda"
25+
The same letters are at least distance 2 from each other.
26+
*/
27+
public class _358 {
28+
29+
public String rearrangeString(String s, int k) {
30+
Map<Character, Integer> count = new HashMap<>();
31+
for (char c : s.toCharArray()) {
32+
count.put(c, count.getOrDefault(c, 0) + 1);
33+
}
34+
35+
PriorityQueue<Map.Entry<Character, Integer>> heap = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
36+
heap.addAll(count.entrySet());
37+
38+
Queue<Map.Entry<Character, Integer>> waitQueue = new LinkedList<>();
39+
40+
StringBuilder stringBuilder = new StringBuilder();
41+
while (!heap.isEmpty()) {
42+
Map.Entry<Character, Integer> entry = heap.poll();
43+
stringBuilder.append(entry.getKey());
44+
entry.setValue(entry.getValue()-1);
45+
waitQueue.offer(entry);
46+
if (waitQueue.size() < k) continue; //there's only k-1 chars in the waitHeap, not full yet
47+
Map.Entry<Character, Integer> front = waitQueue.poll();
48+
if (front.getValue() > 0) heap.offer(front);
49+
}
50+
51+
return stringBuilder.length() == s.length() ? stringBuilder.toString() : "";
52+
}
53+
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._358;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
/**
8+
* Created by stevesun on 6/8/17.
9+
*/
10+
public class _358Test {
11+
12+
private static _358 test;
13+
14+
@BeforeClass
15+
public static void setup(){
16+
test = new _358();
17+
}
18+
19+
@Test
20+
public void test1(){
21+
System.out.println(test.rearrangeString("aabbcc", 3));
22+
}
23+
24+
@Test
25+
public void test2(){
26+
System.out.println(test.rearrangeString("aaabc", 3));
27+
}
28+
29+
@Test
30+
public void test3(){
31+
System.out.println(test.rearrangeString("aaadbbcc", 2));
32+
}
33+
}

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