[LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
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;
- }
- };
类似题目:
参考资料:
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 题目讲解汇总(持续更新中...)
[LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串的更多相关文章
- [LeetCode] 358. Rearrange String k Distance Apart 按距离k间隔重排字符串
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [Swift]LeetCode358. 按距离为k隔离重排字符串 $ Rearrange String k Distance Apart
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- Leetcode: Rearrange String k Distance Apart
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- [leetcode]243. Shortest Word Distance最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [LeetCode] 244. Shortest Word Distance II 最短单词距离 II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- LeetCode 358. Rearrange String k Distance Apart
原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non ...
随机推荐
- Java 哈希表运用-LeetCode 1 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- Effective前端1:能使用html/css解决的问题就不要使用JS
div{display:table-cell;vertical-align:middle}#crayon-theme-info .content *{float:left}#crayon-theme- ...
- 利用rebase来压缩多次提交
我们可以用Git merge –squash来将分支中多次提交合并到master后,只保留一次提交历史.但是有些提交到github远程仓库中的commit信息如何合并呢? 历史记录 首先我们查看一下m ...
- 前端开发:Javascript中的数组,常用方法解析
前端开发:Javascript中的数组,常用方法解析 前言 Array是Javascript构成的一个重要的部分,它可以用来存储字符串.对象.函数.Number,它是非常强大的.因此深入了解Array ...
- iOS-多线程介绍
一.前言部分 最近在面试,重新温习了一遍多线程,希望加深一遍对于多线程的理解. 1.什么是进程? 1).要了解线程我们必须先了解进程,通俗来讲进程就是在系统中运行的一个应用程序. 2).每个线程之间是 ...
- EC笔记:第三部分:16成对使用new和delete
我们都知道,申请的资源,使用完毕后要释放.但是这个释放动作,一定要注意. 举个例子,很多人动态分配的资源,在使用之后,往往直接调用了delete,而不管申请资源的时候用的是new还是new[]. 如下 ...
- Java编程里的类和对象
像我们搞计算机这块的,都知道这么一件事,当前的计算机编程语言主要分为两大块,一为面向过程,二为面向对象.Java就是一门纯面向对象的语言.学习了一个月左右的Java,在下对于Java当中的类和对象有了 ...
- 浅谈时钟的生成(js手写代码)
在生成时钟的过程中自己想到布置表盘的写法由这么几种: 当然利用那种模式都可以实现,所以我们要用一个最好理解,代码有相对简便的方法实现 1.利用三角函数 用js在三角函数布置表盘的过程中有遇见到这种情况 ...
- BI解决方案分享:地产BI数据分析系统的建设
近几年中国地产行业发展迅猛,行业整合已成大势所趋,逐步由区域开发转变为集团化的跨地区综合开发商.然而,对于处在超常规速度发展的房地产企业来说,其面临的挑战也是超常规的.企业要在有限的资金和人力条件下, ...
- iOS 10对隐私权限的管理(必须要改否则会crash)
比如访问的摄像头.麦克风等硬件,都需要提前请求应用权限.允许后才可以使用,或者现在要提前声明,虽然以往要求不严格.比如在iOS10中访问通讯录时,强制必须在Info.plist中加入NSContact ...