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.
public class Solution {
public String rearrangeString(String str, int k) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (int i=0; i<str.length(); i++) {
char c = str.charAt(i);
map.put(c, map.getOrDefault(c, 0) + 1);
}
Queue<Map.Entry<Character, Integer>> maxHeap = new PriorityQueue<>(1, new Comparator<Map.Entry<Character, Integer>>() {
public int compare(Map.Entry<Character, Integer> entry1, Map.Entry<Character, Integer> entry2) {
return entry2.getValue()-entry1.getValue();
}
});
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
maxHeap.offer(entry);
}
Queue<Map.Entry<Character, Integer>> waitQueue = new LinkedList<>();
StringBuilder res = new StringBuilder();
while (!maxHeap.isEmpty()) {
Map.Entry<Character, Integer> entry = maxHeap.poll();
res.append(entry.getKey());
entry.setValue(entry.getValue()-1);
waitQueue.offer(entry);
if (waitQueue.size() >= k) {
Map.Entry<Character, Integer> unfreezeEntry = waitQueue.poll();
if (unfreezeEntry.getValue() > 0) maxHeap.offer(unfreezeEntry);
}
}
return res.length()==str.length()? res.toString() : "";
}
}
Solution2: Greedy Using Array, Time Complexity: O(N*26)
public class Solution {
public String rearrangeString(String str, int k) {
int[] count = new int[26];
int[] nextValid = new int[26];
for (int i=0; i<str.length(); i++) {
count[str.charAt(i)-'a']++;
}
StringBuilder res = new StringBuilder();
for (int index=0; index<str.length(); index++) {
int nextCandidate = findNextValid(count, nextValid, index);
if (nextCandidate == -1) return "";
else {
res.append((char)('a' + nextCandidate));
count[nextCandidate]--;
nextValid[nextCandidate] += k;
}
}
return res.toString();
}
public int findNextValid(int[] count, int[] nextValid, int index) {
int nextCandidate = -1;
int max = 0;
for (int i=0; i<count.length; i++) {
if (count[i]>max && index>=nextValid[i]) {
max = count[i];
nextCandidate = i;
}
}
return nextCandidate;
}
}
Leetcode: Rearrange String k Distance Apart的更多相关文章
- [LeetCode] 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 ...
- 358. Rearrange String k Distance Apart
/* * 358. Rearrange String k Distance Apart * 2016-7-14 by Mingyang */ public String rearrangeString ...
- LC 358. Rearrange String k Distance Apart
Given a non-empty string s and an integer k, rearrange the string such that the same characters are ...
- LeetCode 358. Rearrange String k Distance Apart
原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non ...
- [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】358. Rearrange String k Distance Apart 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rearrang ...
- [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] Reorganize String 重构字符串
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [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 ...
随机推荐
- VMware下虚拟机的转移
将虚拟机文件夹整个拷贝到另一台电脑上: 打开.vmx文件 打开即可: 注意:Mac系统的虚拟机要先用unlocker206破解,才能运行OS系统.
- 08 Servlet
Servlet技术 * Servlet开发动态的Web资源的技术. * Servlet技术 * 在javax. ...
- devexpress 安装及破解
安装dx DXperience-10.1.4 破解文件下载
- Sass和compass 安装 和配合grunt实时显示 [Sass和compass学习笔记]
demo 下载http://vdisk.weibo.com/s/DOlfkrAWjkF/1401192855 为什么要学习Sass和compass ?提高站独立和代码产品化的绝密武器,尤其是程序化cs ...
- osg中内嵌QtBrowser
最近看到osg Examples的osgQtBrowser例子, 觉得效果还是挺好的, 想加入到自己的项目中来, 就这样的搬运工作也出问题了-__- 拷过来的是这一段: osg::ref_ptr< ...
- Multiple annotations found at this line
Multiple annotations found at this line 在使用MyEclipse的时候,通过MVN导入项目时候,webapp下面的JSP页面报了如下的错误: 这种情况通常的原因 ...
- 水坑式攻击-APT攻击常见手段
所谓“水坑攻击”,是指黑客通过分析被攻击者的网络活动规律,寻找被攻击者经常访问的网站的弱点,先攻下该网站并植入攻击代码,等待被攻击者来访时实施攻击. 水坑攻击属于APT攻击的一种,与钓鱼攻击相比,黑客 ...
- 02.JavaScript基础上
JavaScript组成 ECMAScript:解释器.翻译 .平时我们写的代码都是用英文数字之类,而计算机只能读懂0和1,ECMAScript可以把我们写的翻译给计算机,把计算机写的传达给我们DOM ...
- 20145205 《Java程序设计》第8周学习总结
教材学习内容总结 第十五章 通用API 15.1 日志 日志API简介 java.util.logging包提供了日志功能相关类与接口,不必额外配置日志组件,就可在标准Java平台使用是其好处.使用日 ...
- .net手机号码归属地查询
调用百度 api http://apistore.baidu.com/apiworks/servicedetail/117.html 贴上代码 using Newtonsoft.Json;using ...