Rearrange a string so that all same characters become d distance away minDistance priority queue
来源:互联网 发布:英伦对决影评知乎 编辑:程序博客网 时间:2024/05/01 01:08
Given a string and a positive integer d. Some characters may be repeated in the given string. Rearrange characters of the given string such that the same characters become d distance away from each other. Note that there can be many possible rearrangements, the output should be one of the possible rearrangements. If no such arrangement is possible, that should also be reported.
Expected time complexity is O(n) where n is length of input string.
**Examples:
Input: “abb”, d = 2
Output: “bab”
Input: “aacbbc”, d = 3
Output: “abcabc”
Input: “geeksforgeeks”, d = 3
Output: egkegkesfesor
Input: “aaa”, d = 2
Output: Cannot be rearranged**
Solution: The idea is to count frequencies of all characters and consider the most frequent character first and place all occurrences of it as close as possible. After the most frequent character is placed, repeat the same process for remaining characters.
Use HashMap for storing appearance, then priority queue of map.entry, comparator looks like this (max heap, descending order):
private class CompareByValue implements Comparator<Map.Entry<Cell, Integer>> { @Override public int compare(Entry<Cell, Integer> lhs, Entry<Cell, Integer> rhs) { return rhs.getValue().compareTo(lhs.getValue()); }}
http://www.geeksforgeeks.org/rearrange-a-string-so-that-all-same-characters-become-at-least-d-distance-away/
max heap is implemented with priority queue
priority Q example:
// Test.javaimport java.util.Comparator;import java.util.PriorityQueue;public class Test{ public static void main(String[] args) { Comparator<String> comparator = new StringLengthComparator(); PriorityQueue<String> queue = new PriorityQueue<String>(10, comparator); queue.add("short"); queue.add("very long indeed"); queue.add("medium"); while (queue.size() != 0) { System.out.println(queue.remove()); } }}// StringLengthComparator.javaimport java.util.Comparator;public class StringLengthComparator implements Comparator<String>{ @Override public int compare(String x, String y) { // Assume neither string is null. Real code should // probably be more robust // You could also just return x.length() - y.length(), // which would be more efficient. if (x.length() < y.length()) { return -1; } if (x.length() > y.length()) { return 1; } return 0; }}
- Rearrange a string so that all same characters become d distance away minDistance priority queue
- To rearrange the bricks such that all stacks have the same height.
- LeetCode 358. Rearrange String k Distance Apart
- 【LeetCode 358】 Rearrange String k Distance Apart
- A Priority Queue
- LeetCode 358. Rearrange String k Distance Apart(字符间隔)
- [leetcode] 358. Rearrange String k Distance Apart 解题报告
- [Leetcode] 358. Rearrange String k Distance Apart 解题报告
- Rearrange a String With Integers-LintCode
- 【1】Detect if a string has all unique characters
- determine if a string has all unique characters
- Determine if a string has all unique characters
- Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- Print all nodes that are at distance k from a leaf node
- priority queue
- [Leetcode]358. Rearrange String k Distance Apart (贪心,窗口,堆,字符)
- Print all possible strings of length k that can be formed from a set of n characters
- 1.1 Implement an algorithm to determine if a string has all unique characters.
- 5、超链接
- 赋值运算符函数
- Android 嵌入网页,为H5开发做伏笔
- C++ 预定义显示文件和行数的调试技术
- sql 经典命令行建立数据库 (增删改查)
- Rearrange a string so that all same characters become d distance away minDistance priority queue
- 准备ndk环境
- 中国省份及其地级市整理JSON版(2015-08-23)
- STL 之 deque的用法
- HDU 5438 Ponds (拓扑排序应用+DFS)
- ng-class用法
- 前台向后台传递中文参数,防止乱码
- FAT12中,如何定位大于一个扇区(512B)的文件内容
- [BZOJ 3040] 最短路(road) · 堆优化dijkstra