Rearrange a string so that all same characters become d distance away
2014-11-08 14:54
417 查看
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
摘自:http://www.geeksforgeeks.org/rearrange-a-string-so-that-all-same-characters-become-at-least-d-distance-away/
这题就是贪心算法。对于字符串处理来讲,如果不care最终的符号序,就要以考虑用统计重排的方法。
1. 统计所有字符的出现频率;
2. 按出现频率从高到低优先填好所有字符;
如果全部都是ASCII码,共出现m个不同字符,第2步,可以:
1. 用最大堆来做,也可以直接排序,时间复杂度都是O(n+mlgm),空间复杂度就是O(m)。因为m<256<<n,所以最终复杂度也可以说是O(n)。
2. 也可以直接用一个vector<list<char> >记录每个频率下的字符来做。用vector<list<char> >就是O(2n)的时间复杂度了。空间复杂度高点,O(n)。
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
摘自:http://www.geeksforgeeks.org/rearrange-a-string-so-that-all-same-characters-become-at-least-d-distance-away/
这题就是贪心算法。对于字符串处理来讲,如果不care最终的符号序,就要以考虑用统计重排的方法。
1. 统计所有字符的出现频率;
2. 按出现频率从高到低优先填好所有字符;
如果全部都是ASCII码,共出现m个不同字符,第2步,可以:
1. 用最大堆来做,也可以直接排序,时间复杂度都是O(n+mlgm),空间复杂度就是O(m)。因为m<256<<n,所以最终复杂度也可以说是O(n)。
2. 也可以直接用一个vector<list<char> >记录每个频率下的字符来做。用vector<list<char> >就是O(2n)的时间复杂度了。空间复杂度高点,O(n)。
相关文章推荐
- Rearrange a string so that all same characters become d distance away minDistance priority queue
- Rearrange a string so that all same characters become d distance away
- [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
- LeetCode Rearrange String k Distance Apart
- Leetcode: Rearrange String k Distance Apart
- Rearrange String k Distance Apart
- 【LeetCode 358】 Rearrange String k Distance Apart
- 数组空间Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- [android引用他人包出现的错误]All versions of the libraries must be the same at this time.以及It is indirectly re
- Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- Print all possible strings of length k that can be formed from a set of n characters
- a=re.findall('b',c)报错提示:TypeError:expected string or buffer
- ccah-500 第34题 you want to change a configuration parameter so that it affects all six DataNodes
- 1.1 Implement an algorithm to determine if a string has all unique characters.
- 1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures.
- String Reorder Distance apart
- Print all nodes that are at distance k from a leaf node
- 如何使用OpenCV的处理图像,文字变得锐利和清晰?(How to use OpenCV to process image so that the text become sharp and clea
- [LeetCode] String Reorder Distance Apart
- Determine if a string has all unique characters