[Leetcode]358. Rearrange String k Distance Apart (贪心,窗口,堆,字符)

32 篇文章 0 订阅

 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.


题解:贪心。统计每个字符个数,每次优先放字符个数多的。


class Solution {
public:
    string rearrangeString(string str, int k) {   //1604MS
        if(k<=1) return str;
        string res="";
        int len=str.size();
        unordered_set<char> Set;
        map<char,int> Map;
        for(char c:str) {
            Map[c]++;
        }
        bool f=true;
        int ans=0;
        while(true) {
            char nex;
            int t=-1;
            f=false;
            for(int i=0;i<26;i++) {
                char tmp='a'+i;
                if(Map[tmp]<=0) continue;
                if(Set.empty()||Set.find(tmp)==Set.end()) {
                    f=true;
                    if(Map[tmp]>t) {
                        t=Map[tmp];
                        nex=tmp;
                    }
                }
            }
            if(!f) break;
            res+=nex;
            ans++;
            Map[nex]--;
            if(ans==len) {
                f=true;break;
            }
            if(ans>=k) {
                Set.erase(res[ans-k]);
            }
            Set.insert(nex);
        }
        if(!f) return "";
        else return res;
    }
};

class Solution {
public:
    string rearrangeString(string str, int k) {   //128MS
        if(k == 0) return str;
        int length = (int)str.size(); 
        
        string res;
        unordered_map<char, int> dict;
        priority_queue<pair<int, char>> pq;
        
        for(char ch : str) dict[ch]++;
        for(auto it = dict.begin(); it != dict.end(); it++){
            pq.push(make_pair(it->second, it->first));
        }
        
        while(!pq.empty()){
            vector<pair<int, char>> cache; //store used char during one while loop
            int count = min(k, length); //count: how many steps in a while loop
            for(int i = 0; i < count; i++){
                if(pq.empty()) return "";
                auto tmp = pq.top();
                pq.pop();
                res.push_back(tmp.second);
                if(--tmp.first > 0) cache.push_back(tmp);
                length--;
            }
            for(auto p : cache) pq.push(p);
        }
        return res;
    }
};


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值