Leetcode: Rearrange String k Distance Apart

Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other.

All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string "".

Example 1:
str = "aabbcc", k = 3

Result: "abcabc"

The same letters are at least distance 3 from each other.
Example 2:
str = "aaabc", k = 3 

Answer: ""

It is not possible to rearrange the string.
Example 3:
str = "aaadbbcc", k = 2

Answer: "abacabcd"

Another possible answer is: "abcabcda"

The same letters are at least distance 2 from each other.
 Analysis:
Solution 1:
The greedy algorithm is that in each step, select the char with highest remaining count if possible (if it is not in the waiting queue).  Everytime, we add the char X with the largest remaining count, after that we should put it back to the queue (named readyQ) to find out the next largest char. HOWEVER, do not forget the constraint of K apart. So we should make char X waiting for K-1 arounds of fetching and then put it back to queue. We use another queue (named waitingQ) to store the waiting chars. Whenever, the size of this queue equals to K, the head char is ready to go back to readyQ.
 
Solution 2:
Based on solution 1, we do not use PriorityQueue, instead, we just use array with 26 elements to store chars' count and its next available position. Every round, we iterate through the arrays and find out the available char with max count.
 
NOTE: theoretically, the complexity of solution 1 using PriorityQueue is O(nlog(26)), while the complexity of solution 2 is O(n*26) which is larger than solution 1. HOWEVER, in real implementation, because solution 1 involves creating more complex data structures and sorting them, soluiont 1 is much slower than solution 2.
 
Solution 1: Greedy Using Heap, Time Complexity: O(Nlog(26))
What I learn: Map.Entry can be a very good Wrapper Class, you can directly use it to implement heap without writing a wrapper class yourself
 1 public class Solution {
 2     public String rearrangeString(String str, int k) {
 3         Map<Character, Integer> map = new HashMap<Character, Integer>();
 4         for (int i=0; i<str.length(); i++) {
 5             char c = str.charAt(i);
 6             map.put(c, map.getOrDefault(c, 0) + 1);
 7         }
 8         Queue<Map.Entry<Character, Integer>> maxHeap = new PriorityQueue<>(1, new Comparator<Map.Entry<Character, Integer>>() {
 9             public int compare(Map.Entry<Character, Integer> entry1, Map.Entry<Character, Integer> entry2) {
10                 return entry2.getValue()-entry1.getValue();
11             }
12         
13         });
14         for (Map.Entry<Character, Integer> entry : map.entrySet()) {
15             maxHeap.offer(entry);
16         }
17         Queue<Map.Entry<Character, Integer>> waitQueue = new LinkedList<>();
18         StringBuilder res = new StringBuilder();
19         
20         while (!maxHeap.isEmpty()) {
21             Map.Entry<Character, Integer> entry = maxHeap.poll();
22             res.append(entry.getKey());
23             entry.setValue(entry.getValue()-1);
24             waitQueue.offer(entry);
25             if (waitQueue.size() >= k) {
26                 Map.Entry<Character, Integer> unfreezeEntry = waitQueue.poll();
27                 if (unfreezeEntry.getValue() > 0) maxHeap.offer(unfreezeEntry);
28             }
29         }
30         return res.length()==str.length()? res.toString() : "";
31     }
32 }

 

Solution2: Greedy Using Array, Time Complexity: O(N*26)

 1 public class Solution {
 2     public String rearrangeString(String str, int k) {
 3         int[] count = new int[26];
 4         int[] nextValid = new int[26];
 5         for (int i=0; i<str.length(); i++) {
 6             count[str.charAt(i)-'a']++;
 7         }
 8         StringBuilder res = new StringBuilder();
 9         for (int index=0; index<str.length(); index++) {
10             int nextCandidate = findNextValid(count, nextValid, index);
11             if (nextCandidate == -1) return "";
12             else {
13                 res.append((char)('a' + nextCandidate));
14                 count[nextCandidate]--;
15                 nextValid[nextCandidate] += k;
16             }
17         }
18         return res.toString();
19     }
20     
21     public int findNextValid(int[] count, int[] nextValid, int index) {
22         int nextCandidate = -1;
23         int max = 0;
24         for (int i=0; i<count.length; i++) {
25             if (count[i]>max && index>=nextValid[i]) {
26                 max = count[i];
27                 nextCandidate = i;
28             }
29         }
30         return nextCandidate;
31     }
32 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值