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


题目地址:https://leetcode.com/problems/reorganize-string/description/

题目描述:

Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.

If possible, output any possible result. If not possible, return the empty string.

Example 1:

Input: S = "aab"
Output: "aba"

Example 2:

Input: S = "aaab"
Output: ""

Note:

  1. S will consist of lowercase letters and have length in range [1, 500].

题目大意

输入是一个字符串,如果这个字符串重新排列之后能组成一个新字符串使得这个字符串相邻的字符都不相同,那么返回这个新字符串;如果做不到,就返回空字符串。

解题方法

这个题我知道要用Counter,而且也知道要做多次操作,可是我想的是递归,没做出来。。看了书影博客,才做出来的。

方法是:

统计这个字符串每个字符的出现频率,在组成新字符串时,在满足和上一个字符不相同的情况下,优先使用最流行的字符串。每用一个字符都对counter进行更新,直至循环结束。注意如果次数为0要删除,否则counter不会为空。

class Solution(object):
def reorganizeString(self, S):
"""
:type S: str
:rtype: str
"""
counter = collections.Counter(S)
ans = "#"
while counter:
stop = True
for item, times in counter.most_common():
if ans[-1] != item:
ans += item
counter[item] -= 1
if not counter[item]:
del counter[item]
stop = False
break
if stop: break
return ans[1:] if len(ans) == len(S) + 1 else ""

其实这个题和358. Rearrange String k Distance Apart题目一模一样的,都是使用小根堆+优先使用出现次数多的字符方法进行模拟。

需要留意的地方是出现的次数是正的,python的堆是小根堆,所以进堆的时候把次数取了符号,这样的话,中间对次数-1的步骤实际上使用的是+1。另外,已经使用过了的字符这轮就不会再取了,所以使用temp临时保存一下。

代码也基本相同,如下:

class Solution(object):
def reorganizeString(self, S):
"""
:type S: str
:rtype: str
"""
_len = len(S)
count = collections.Counter(S)
que = [(-v, c) for c, v in count.items()]
heapq.heapify(que)
res = ""
while _len:
cnt = min(_len, 2)
temp = list()
for i in range(cnt):
if not que:
return ""
v, c = heapq.heappop(que)
res += c
if v + 1 != 0:
temp.append((v + 1, c))
_len -= 1
for x in temp:
heapq.heappush(que, x)
return res

日期

2018 年 6 月 13 日 ———— 腾讯赛圆满结束!两个月修得正果哈哈~~
2018 年 10 月 14 日 —— 周赛做出来3个题,开心

【LeetCode】767. Reorganize String 解题报告(Python)的更多相关文章

  1. [LeetCode] 767. Reorganize String 重构字符串

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  2. LeetCode - 767. Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  3. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  4. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  5. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  6. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  7. [leetcode]Weekly Contest 68 (767. Reorganize String&&769. Max Chunks To Make Sorted&&768. Max Chunks To Make Sorted II)

    766. Toeplitz Matrix 第一题不说,贼麻瓜,好久没以比赛的状态写题,这个题浪费了快40分钟,我真是...... 767. Reorganize String 就是给你一个字符串,能不 ...

  8. 767. Reorganize String - LeetCode

    Question 767. Reorganize String Solution 题目大意: 给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串. 思路: 首先找 ...

  9. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

随机推荐

  1. 在Linux下搭建nRF51822的开发烧写环境(makefile版)

    http://www.qingpingshan.com/m/view.php?aid=394836

  2. SQL-用到的数据库语句总结

    0.SELECT * FROM  CHARACTER_SETS LIMIT 0,10   #从CHARACTER_SETS表中,从第1行开始,提取10行[包含第1行] 1.SELECT * FROM  ...

  3. MybatisPlus使用Wrapper实现查询功能

    Wrapper---条件查询器 :使用它可以实现很多复杂的查询 几个案例 环境: 参照博客:MybatisPlus入门程序 1.条件查询 1.1 查询name不为空的用户,并且邮箱不为空的用户,年龄大 ...

  4. 在Kubernetes上安装Percona XtraDB集群

    官方文档地址:https://www.percona.com/doc/kubernetes-operator-for-pxc/kubernetes.html 一.简介 Percona XtraDB C ...

  5. gitlab之实战部署

    #:准备Java环境,安装jdk root@ubuntu:~# cd /usr/local/src/ root@ubuntu:/usr/local/src# ls jdk-8u191-linux-x6 ...

  6. 实现将rsyslog将日志记录与MySQL中

    准备两个节点 node3:  rsyslog node2:   数据库 准备相应的包 [root@node3 php-fpm.d]#yum install rsyslog-mysql 将数据拷贝到数据 ...

  7. linux shell学习之shell流程控制

    在linux shell编程中,流程控制结构与语句,也算是shell脚本中的重点了,不了解的朋友,跟随脚本小编一起来学习下吧. linux控制流结构学习. 一,shell控制流结构 1.控制结构   ...

  8. Activiti工作流引擎使用详解(一)

    一.IDEA安装activiti插件 在插件库中查找actiBPM,安装该插件,如果找不到该插件,请到插件库中下载该包手动安装,插件地址 http://plugins.jetbrains.com/pl ...

  9. zookeeper 异常 :stat is not executed because it is not in the whitelist. Connection closed b

    1 .问题 1.启动 zookeeper 后 用指令:  telnet 127.0.0.1 2181 连接 提示输入指令 :stat 后报错,然后关闭连接 2.问题解决: 修改启动指令 zkServe ...

  10. Consumer方法结合Lambda表达式的应用

    package com.itheima.demo05.Consumer;import java.util.function.Consumer;/** * @author newcityman * @d ...