题目如下:

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/4 times where n is 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 s balanced.

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^5
  • s.length is a multiple of 4
  • contains 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的更多相关文章

  1. LeetCode 1234. Replace the Substring for Balanced String

    原题链接在这里:https://leetcode.com/problems/replace-the-substring-for-balanced-string/ 题目: You are given a ...

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

  3. 【LeetCode】387. First Unique Character in a String

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...

  4. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  5. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  6. 【LeetCode】648. Replace Words 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 字典 前缀树 日期 题目地址:https:/ ...

  7. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  8. 【LeetCode】5. Longest Palindromic Substring 最大回文子串

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  9. 【leetcode】5. Longest Palindromic Substring

    题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

随机推荐

  1. 【计算机视觉】论文笔记:Ten years of pedestrian detection, what have we learned?

    最近正在研究行人检测,学习了一篇2014年发表在ECCV上的一篇综述性的文章,是对行人检测过去十年的一个回顾,从dataset,main approaches的角度分析了近10年的40多篇论文提出的方 ...

  2. w 命令

    NAME w - Show who is logged on and what they are doing. SYNOPSIS w - [husfiV] [user] 参数说明: -f 开启或关闭显 ...

  3. 云数据库 MongoDB版

    阿里云云数据库MongoDB版是一种安全可靠.可弹性伸缩的云数据库服务,目前支持ReplicaSet和Sharding两种部署架构,通过简单的几步操作即可快速部署.阿里云云数据库MongoDB版是一种 ...

  4. 云数据库RDS SQL Server 版

    云数据库RDS SQL Server版是一种可弹性伸缩的在线数据库服务,并具备自动监控.备份.容灾恢复等方面的全套解决方案,彻底解决数据库运维的烦恼 请观看视频简介 SQL Server是发行最早的商 ...

  5. 批量删除Maven本地仓库中未下载完成的jar包(不完整的jar包)

    1.删除repository库目录下所有后缀名是.lastUpdated的文件 2.进入maven本地仓库地址: CMD进入windows的路径(或在仓库目录的地址栏直接输入CMD,回车自动打开); ...

  6. # jsp及servlet学习笔记

    目录 jsp及servlet学习笔记 JSP(Java Server Page Java服务端网页) 指令和动作: servlet(小服务程序) jsp及servlet学习笔记 JSP(Java Se ...

  7. |、&、||、&&、^符号含义

    |和&为计算机中二进制之间的位运算 在计算机中二进制的0表示false,1表示true. |为位运算中的或运算:它的运算逻辑为一真则真,全假则假 &为位运算中的并运算:它的运算逻辑为一 ...

  8. springboot日志框架学习------slf4j和log4j2

    springboot日志框架学习------slf4j和log4j2 日志框架的作用,日志框架就是用来记录系统的一些行为的,可以通过日志发现一些问题,在出现问题之后日志是好的一个帮手. 市面上的日志框 ...

  9. b/s和c/s

    一.B/S结构 B是英文单词“Browser”的首字母,即浏览器的意思:S是英文单词“Server”的首字母,即服务器的意思.B/S就是“Browser/Server”的缩写,即“浏览器/服务器”模式 ...

  10. vue 组件基本使用

    组件的基本使用 注册组件 注册组件就是利用Vue.component()方法,先传入一个自定义组件的名字,然后传入这个组件的配置.vue.component()注册的全局组件 Vue.componen ...