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)。

Rearrange a string so that all same characters become d distance away的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. LeetCode 358. Rearrange String k Distance Apart

    原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non ...

  5. [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 ...

  6. 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 ...

  7. 【LeetCode】358. Rearrange String k Distance Apart 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rearrang ...

  8. string stack操作要注重细节问题

    A string S consisting of N characters is considered to be properly nested if any of the following co ...

  9. Python 字符串操作及string模块使用

    python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求: python的字符串属性函数 python的string模块 1.字符串属性方法操作: 1.>字符串格式输出对 ...

随机推荐

  1. Regionals 2012 :: Chengdu

    题目连接 排行榜 A和I都是签到题 按位BFS K Yet Another Multiple Problem 题意:给一些可以用的数字,求最小的数,它由特定的数字组成且是n的倍数 分析:暴力枚举不可行 ...

  2. WPF中ComboBox用法

    The ComboBox control is in many ways like the ListBox control, but takes up a lot less space, becaus ...

  3. WPF: 旋转Thumb后,DragDelta移动距离出错的解决

    当Thumb跟随Grid旋转90度后,拖拽控件时会飞掉. <Grid x:Name="gridMain" Width="100" Height=" ...

  4. 百度地图API使用记录

    用户数据图层的总教程: 就是把用户数据存到LBS云里面,应用从云里面读数据 http://developer.baidu.com/map/jsdevelop-9.htm 上传数据的地方: http:/ ...

  5. CF# Educational Codeforces Round 3 A. USB Flash Drives

    A. USB Flash Drives time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. java 程序访问hdfs错误 hadoop2.2.0

    很奇怪的问题,程序在eclipse上跑没问题: 这就代码:FileSystem fs = FileSystem.get(URI.create(hdfs_file),  conf , "use ...

  7. spring aop两种配置方式(1)

    第一种:注解配置AOP注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before ...

  8. 栈式自动编码器(Stacked AutoEncoder)

    起源:自动编码器 单自动编码器,充其量也就是个强化补丁版PCA,只用一次好不过瘾. 于是Bengio等人在2007年的  Greedy Layer-Wise Training of Deep Netw ...

  9. CentOS 命令【备忘】

    1.查看物理cpu个数 grep 'physical id' /proc/cpuinfo | sort -u | wc -l 2.查看核心数量 grep 'core id' /proc/cpuinfo ...

  10. BZOJ 3732 题解

    3732: Network Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ...