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.

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

Example 1:

Input: s = "aabbcc", k = 3
Output: "abcabc"
Explanation: The same letters are at least distance 3 from each other.

Example 2:

Input: s = "aaabc", k = 3
Output: ""
Explanation: It is not possible to rearrange the string.

Example 3:

Input: s = "aaadbbcc", k = 2
Output: "abacabcd"
Explanation: 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.

这道题给了我们一个字符串str,和一个整数k,让我们对字符串str重新排序,使得其中相同的字符之间的距离不小于k,这道题的难度标为Hard,看来不是省油的灯。的确,这道题的解法用到了哈希表,堆,和贪婪算法。这道题我最开始想的算法没有通过OJ的大集合超时了,下面的方法是参考网上大神的解法,发现十分的巧妙。我们需要一个哈希表来建立字符和其出现次数之间的映射,然后需要一个堆来保存这每一堆映射,按照出现次数来排序。然后如果堆不为空我们就开始循环,我们找出k和str长度之间的较小值,然后从0遍历到这个较小值,对于每个遍历到的值,如果此时堆为空了,说明此位置没法填入字符了,返回空字符串,否则我们从堆顶取出一对映射,然后把字母加入结果res中,此时映射的个数减1,如果减1后的个数仍大于0,则我们将此映射加入临时集合v中,同时str的个数len减1,遍历完一次,我们把临时集合中的映射对由加入堆中,参见代码如下:

class Solution {
public:
string rearrangeString(string str, int k) {
if (k == ) return str;
string res;
int len = (int)str.size();
unordered_map<char, int> m;
priority_queue<pair<int, char>> q;
for (auto a : str) ++m[a];
for (auto it = m.begin(); it != m.end(); ++it) {
q.push({it->second, it->first});
}
while (!q.empty()) {
vector<pair<int, int>> v;
int cnt = min(k, len);
for (int i = ; i < cnt; ++i) {
if (q.empty()) return "";
auto t = q.top(); q.pop();
res.push_back(t.second);
if (--t.first > ) v.push_back(t);
--len;
}
for (auto a : v) q.push(a);
}
return res;
}
};

类似题目:

Task Scheduler

参考资料:

https://leetcode.com/problems/rearrange-string-k-distance-apart/

https://leetcode.com/discuss/108174/c-unordered_map-priority_queue-solution-using-cache

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. Myeclipse8.6配置android_SDK,进行android开发(转载)
  2. Apache Commons Collections
  3. JS 之继承
  4. VC++编译libpng
  5. dl,dt,dd,ul,li,ol区别
  6. c# 浏览器区别
  7. zookeeper_03:Java 客户端(原生API)
  8. ubuntu 14.04 64位安装HTK3.5
  9. Wechat 微信端调用“微信支付接口”的正确方式
  10. 博弈论中的Nim博弈
  11. IDT表连接
  12. MongoDB分片详解
  13. python中的os.listdir()函数
  14. node出现 Error: listen EACCES 0.0.0.0:8080错误
  15. Get filename from URL using Javascript
  16. Python全栈开发-Day5-常用模块学习
  17. MyBatis Generator 生成数据库自带中文注释
  18. Linux命令中,$、#、@、0、1、2、*、?的作用
  19. 关于空指针NULL、野指针、通用指针
  20. ZJOI2018 round^2 游记

热门文章

  1. GitHub更新自己Fork的项目
  2. WPF binding 参考
  3. alienware Win8 系统安装
  4. 【Spring】SpringMVC中浅析Date类型数据的传递
  5. 企业管理软件ERP演变之一
  6. ActiveMQ 简单搭建
  7. 通过JAXB完成Java对象与XML之间的转换
  8. Java设计模式——线程安全的单件模式
  9. window下的各种宽高度小结
  10. 【转】推荐10款最热门jQuery UI框架