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.

Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.

 Analysis:
Solution 1:
Everytime, we add the char X with the largest 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:
import java.util.Map.Entry;

public class Solution {
    public String rearrangeString(String str, int k) {
        if (str.isEmpty() || k<0) return "";
        if (k==0) return str;
        
        HashMap<Character,Integer> charMap = new HashMap<Character,Integer>();
        for (char c : str.toCharArray()){
            charMap.put(c,charMap.getOrDefault(c,0)+1);
        }
        
        char[] chars = new char[charMap.size()];
        int[] charCount = new int[charMap.size()];
        int[] valid = new int[charMap.size()];
        int index = 0;
        for (Entry entry : charMap.entrySet()){
            char c = (char) entry.getKey();
            int count = (int) entry.getValue();
            chars[index] = c;
            charCount[index++] = count;
        }
            
        StringBuilder builder = new StringBuilder();
        int nextInd = 0;
        int nextChar = getNextChar(charCount,valid,nextInd);
        while (nextChar!=-1){
            builder.append(chars[nextChar]);
            charCount[nextChar]--;
            valid[nextChar] = (nextInd++) + k;
            nextChar = getNextChar(charCount,valid,nextInd);
        }
        if (builder.length()!=str.length()) return "";
        
        return builder.toString();
    }
    
    public int getNextChar(int[] charCount, int[] valid, int nextInd){
        int maxCount = 0;
        int index = -1;
        for (int i=0;i<charCount.length;i++)
            if (valid[i]<=nextInd && charCount[i]>maxCount){
                maxCount = charCount[i];
                index = i;
            }
        return index;
    }
}

 

Solution 2:

import java.util.Map.Entry;

public class Solution {
    public String rearrangeString(String str, int k) {
        if (str.isEmpty() || k<0) return "";
        if (k==0) return str;
        
        int[] charCount = new int[26];
        int[] valid = new int[26];
        Arrays.fill(valid,-1);
        for (char c : str.toCharArray()){
            charCount[c-'a']++;
        }
        for (int i=0;i<26;i++)
            if (charCount[i]>0){
                valid[i] = 0;
            }
            
        StringBuilder builder = new StringBuilder();
        int nextInd = 0;
        int nextChar = getNextChar(charCount,valid,nextInd);
        while (nextChar!=-1){
            builder.append((char)(nextChar+'a'));
            charCount[nextChar]--;
            valid[nextChar] = (nextInd++) + k;
            nextChar = getNextChar(charCount,valid,nextInd);
        }
        if (builder.length()!=str.length()) return "";
        
        return builder.toString();
    }
    
    public int getNextChar(int[] charCount, int[] valid, int nextInd){
        int maxCount = 0;
        int index = -1;
        for (int i=0;i<charCount.length;i++)
            if (valid[i]<=nextInd && charCount[i]>maxCount){
                maxCount = charCount[i];
                index = i;
            }
        return index;
    }
}

 

 
 

转载于:https://www.cnblogs.com/lishiblog/p/5856198.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值