【LeetCode】767. Reorganize String 解题报告(Python)
作者: 负雪明烛
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:
- 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)的更多相关文章
- [LeetCode] 767. Reorganize String 重构字符串
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- LeetCode - 767. Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- [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 就是给你一个字符串,能不 ...
- 767. Reorganize String - LeetCode
Question 767. Reorganize String Solution 题目大意: 给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串. 思路: 首先找 ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
随机推荐
- A Child's History of England.43
PART THE SECOND When the King heard how Thomas à Becket had lost his life in Canterbury Cathedral, t ...
- 8. LINUX shell 环境变量
wc –l file 计算文件行数, wc -w file 计算文件中的单词数, wc -c file 计算文件中的字符数 查看文件内容: cat .more
- c学习 - 第三章:数据类型、运算符与表达式
数据类型 基本类型 整型 短整型(short int) 基本整型(int) 长整型(long int) 字符型(char) 浮点型 单精度(float) 双精度(double) 长双精度(long d ...
- 用户创建firefox配置文件
1.打开cmd进放 firefox.exe所在的目录 如:D:\>cd D:\Mozilla Firefox 2.运行如命令:D:\Mozilla Firefox>firefox.exe ...
- 分布式系统为什么不用自增id,要用雪花算法生成id???
1.为什么数据库id自增和uuid不适合分布式id id自增:当数据量庞大时,在数据库分库分表后,数据库自增id不能满足唯一id来标识数据:因为每个表都按自己节奏自增,会造成id冲突,无法满足需求. ...
- Leetcode 78题-子集
LeetCode 78 网上已经又很多解这题的博客了,在这只是我自己的解题思路和自己的代码: 先贴上原题: 我的思路: 我做题的喜欢在本子或别处做写几个示例,以此来总结规律:下图就是我从空数组到数组长 ...
- ActiveMQ(二)——ActiveMQ的安装和基本使用
一:安装 2.启动之后成功 二.创建实例测试ActiveMQ 配置Maven所需的依赖 <dependency> <groupId>org.apache.activemq< ...
- 从零开始写一个前端脚手架四、初始化进程提示(chalk)
我们之前说过bin里面的index.js文件是作为入口文件存在的.实际上的初始化内容在.action里面操作的,为了方便管理,我们把实际操作的代码抽出来放一块儿管理 创建指令文件 在根目录创建一个co ...
- [BUUCTF]REVERSE——[BJDCTF 2nd]8086
[BJDCTF 2nd]8086 附件 步骤: 首先查壳儿,无壳,直接上ida,检索字符串,程序里就一个字符串 没法f5反编译出伪代码,大致看了一下汇编,start函数之后调用了sub_10030函数 ...
- BUU | pwnable_orw
题解网上其他师傅已经写过了而且写的很详细,菜鸡只好写一下自己做题中的笔记 Payload : #coding:utf-8 from pwn import * context(log_level = ' ...