【leetcode】1234. Replace the Substring for Balanced String
题目如下:
You are given a string containing only 4 kinds of characters
'Q','W', 'E'and'R'.A string is said to be balanced if each of its characters appears
n/4times wherenis the length of the string.Return the minimum length of the substring that can be replaced with any other string of the same length to make the original string
sbalanced.Return 0 if the string is already balanced.
Example 1:
Input: s = "QWER"
Output: 0
Explanation: s is already balanced.Example 2:
Input: s = "QQWE"
Output: 1
Explanation: We need to replace a 'Q' to 'R', so that "RQWE" (or "QRWE") is balanced.Example 3:
Input: s = "QQQW"
Output: 2
Explanation: We can replace the first "QQ" to "ER".Example 4:
Input: s = "QQQQ"
Output: 3
Explanation: We can replace the last 3 'Q' to make s = "QWER".Constraints:
1 <= s.length <= 10^5s.lengthis a multiple of4scontains only'Q','W','E'and'R'.
解题思路:如果字符串需要替换后才能到达平衡,那么说明至少有一个字符出现的次数超过1/4,当然至多也只有三个字符出现的次数都超过1/4。假设x1...xn为超过1/4的字符,那么就需要将xn们替换成不超过1/4的字符,具体每个xn需要替换的次数为 xn出现的次数 - len(s)/4,记为dn。所以题目就转换成找出一个最短的字符串,使得至少要包含dn的xn。如下图,输入为:WQWRQQQW,很容易可以求出每个字符在[0~i]区间内出现的个数,假设符合题目要求的子字符串是从下标i开始,只需要找出Q[i] + dn出现的下标j,即表示在i~j区间内Q出现的个数满足最小需要删除的个数,因为Q在区间内出现的次数有序,这里可以用二分查找,同时求出其他超过1/4的字符对应出现的下标,并求出这些下标的最大值,这个最大值就是子字符串是从下标i开始满足题目要求的子字符串最小的长度,遍历整个输入,求出[0~len(s)]区间内最小的长度即可。

代码如下:
class Solution(object):
def balancedString(self, s):
"""
:type s: str
:rtype: int
"""
dic = {'Q':{},'W':{},'E':{},'R':{}}
char = ['Q','W','E','R']
char_count = [0,0,0,0]
s = s.replace('\n','')
for i in range(len(s)):
inx = char.index(s[i])
char_count[inx] += 1
dic[s[i]][char_count[inx]] = i res = float('inf')
char_count_2 = [0, 0, 0, 0]
for i in range(len(s)):
inx = char.index(s[i])
if char_count[inx] <= len(s)/4:
continue
count = -float('inf')
for j in range(len(char)):
if char_count[j] <= len(s)/4:
continue
diff = char_count[j] - len(s)/4
if char_count_2[j] + diff in dic[char[j]]:
end = dic[char[j]][char_count_2[j] + diff]
count = max(count, end - i +1)
else:
count = -float('inf')
break
if count != -float('inf'):
res = min(res,count)
char_count_2[inx] += 1
return res if res != float('inf') else 0
【leetcode】1234. Replace the Substring for Balanced String的更多相关文章
- LeetCode 1234. Replace the Substring for Balanced String
原题链接在这里:https://leetcode.com/problems/replace-the-substring-for-balanced-string/ 题目: You are given a ...
- 【LeetCode】76. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- 【LeetCode】648. Replace Words 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 字典 前缀树 日期 题目地址:https:/ ...
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- 【LeetCode】5. Longest Palindromic Substring 最大回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 【leetcode】5. Longest Palindromic Substring
题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
随机推荐
- 无限级根据Id获得所有子节点数据
from sysobjects where id = object_id('tb1') and type = 'U') drop table tb1 go create table tb1 ( Id ...
- Akka入门
原文:http://doc.akka.io/docs/akka/2.3.6/intro/getting-started.html 预备知识 AKKA要求你的计算机已经安装了Java1.6或更高版本. ...
- s7-200日常使用烂笔头
这篇文章只是记录我的苦逼自控之路,有些是书上的,有些是自己发现的,不记载网上得出来的一些东西.只为强化记忆以及便于翻阅. 1.今天使用PC\PPI cable线缆连接成功了200,这个线是盗版线,之前 ...
- sql server第三方产品
sql server第三方产商工具 双活: 1. Moebius for SQL Server :http://www.grqsh.com/Subpage/product_MoebiusDA.html ...
- Chrome的cookie放在哪里了,Cookie/Session机制详解
Chrome的cookie放在哪里了,Cookie/Session机制详解:https://blog.csdn.net/u010002184/article/details/82082951
- 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...
- # vmware异常关机后,虚拟系统无法启动的解决办法
vmware异常关机后,虚拟系统无法启动的解决办法 先使用everything搜索所有后缀为.lck的文件,这些文件全部删除,如果不确定是否可以删除,先把这些文件转移到桌面,等能启动虚拟系统之后再删除 ...
- HTTPS原理(三次握手)
第一步: 客户端向服务器发送HTTPS请求,服务器将公钥以证书的形式发送到客户端(服务器端存放私钥和公钥). 第二步: 浏览器生成一串随机数,然后用公钥对随机数和hash签名进行加密,加密后发送给服务 ...
- mysql转换表的存储引擎方法
如果转换表的存储引擎,将会丢失原存储引擎的所有特性. 例如:如果将innodb转换成myisam,再转回innodb,原innodb表的的外键将丢失. 假设默认存储引擎是MyISAM转为InnoDB ...
- 多边形面积(Area_Of_Polygons)
原理: 任意多边形的面积可由任意一点与多边形上依次两点连线构成的三角形矢量面积求和得出. 分析: 由于给出的点是相对于我们的坐标原点的坐标,每个点实际上我们可以当作一个顶点相对于原点的向量,如下图所示 ...