作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/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 = " ", 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.

题目大意

判断给出的字符串能不能构成一个新的排列,在这个排列中,所有的相同字符之间的间距最少是k.

解题方法

使用Counter统计每个字符出现的次数,然后使用大根堆,每次弹出出现次数最多的字符,添加到生成结果字符串的末尾。如果剩余的不同字符个数不够k,那么说明不能满足题目的要求,返回空字符串。另外,每次弹出出现次数最多的字符之后,不能直接放入堆中,因为直接放入堆中可能下次又被弹出来,所以应该放入一个临时的数组中,在单次操作结束之后再重新插入堆中。

时间复杂度是O(N),空间复杂度是O(N)。

class Solution:
def rearrangeString(self, words, k):
_len = len(words)
words_count = collections.Counter(words)
que = []
heapq.heapify(que)
for w, v in words_count.items():
heapq.heappush(que, (-v, w))
res = ""
while que:
cnt = min(_len, k)
used = []
for i in range(cnt):
if not que:
return ""
v, w = heapq.heappop(que)
res += w
if -v > 1:
used.append((v + 1, w))
_len -= 1
for use in used:
heapq.heappush(que, use)
return res

参考资料:

http://www.cnblogs.com/grandyang/p/5586009.html

日期

2018 年 10 月 13 日 —— 这个题没有用OJ测试

【LeetCode】358. Rearrange String k Distance Apart 解题报告(Python)的更多相关文章

  1. LeetCode 358. Rearrange String k Distance Apart

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

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

  3. 358. Rearrange String k Distance Apart

    /* * 358. Rearrange String k Distance Apart * 2016-7-14 by Mingyang */ public String rearrangeString ...

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

  5. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

  6. 【LeetCode】658. Find K Closest Elements 解题报告(Python)

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

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

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

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

随机推荐

  1. 深度探讨 PHP 之性能

    1.缘起 关于PHP,很多人的直观感觉是PHP是一种灵活的脚本语言,库类丰富,使用简单,安全,非常适合WEB开发,但性能低下.PHP的性能是否真的就 如同大家的感觉一样的差呢?本文就是围绕这么一个话题 ...

  2. JAVA中null,"",equals,==相互之间使用详解

    "equals" 与 "==" "equals"只是比较值是否相同 而"=="则是比较两个变量是不是同一个变量,也应时是 ...

  3. A Child's History of England.21

    There was one tall Norman Knight who rode before the Norman army on a prancing horse, throwing up hi ...

  4. nodeJs,Express中间件是什么与常见中间件

    中间件的功能和分类 中间件的本质就是一个函数,在收到请求和返回相应的过程中做一些我们想做的事情.Express文档中对它的作用是这么描述的: 执行任何代码.修改请求和响应对象.终结请求-响应循环.调用 ...

  5. CSS系列,清除浮动方法总结

    在非IE浏览器(如Firefox)下,当容器的高度为auto,且容器的内容中有浮动(float为left或right)的元素.在这种情况下,容器的高度不能自动伸长以适应内容的高度,使得内容溢出到容器外 ...

  6. C逗号表达式

    c语言提供一种特殊的运算符,逗号运算符,优先级别最低,它将两个及其以上的式子联接起来,从左往右逐个计算表达式,整个表达式的值为最后一个表达式的值.如:(3+5,6+8)称为逗号表达式,其求解过程先表达 ...

  7. When does compiler create default and copy constructors in C++?

    In C++, compiler creates a default constructor if we don't define our own constructor (See this). Co ...

  8. my37_MGR流控对数据库性能的影响以及MGR与主从的性能对比

    mysql> show variables like 'group_replication_flow_control_applier_threshold'; +----------------- ...

  9. 【Java 8】Stream.distinct() 列表去重示例

    在这篇文章里,我们将提供Java8 Stream distinct()示例. distinct()返回由该流的不同元素组成的流.distinct()是Stream接口的方法. distinct()使用 ...

  10. Vuejs-网络

    1.axios是什么 是基于promise用于浏览器和node.js的http客户端一个js库,基于Promise这点要好好理解一下. 2.特点 支持浏览器和node.js 支持promise 能拦截 ...