LeetCode 1234. Replace the Substring for Balanced String
原题链接在这里:https://leetcode.com/problems/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/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 of4
s
contains only'Q'
,'W'
,'E'
and'R'
.
题解:
n = s.length(). If s is balanced, then count of each of Q, W, E, R should be n/4.
The question is asking for minimum length, then it could be sliding window.
We don't need to chare about inside sliding window. We care about outside sliding window.
If count of one of 4 chars is > n/4, then no matter how we replace the chars inside the window, the count of this char is always > n/4.
Thus when doing sliding window, we care about the count of 4 chars, they can't be over n/4.
In order to do that, we need to get count for each char for the whole s first.
Thne runner minus the count by 1.
while the count of 4 chars <=k, update minimum lenth, move walker to increase count by 1.
here walker could be larger than runner.
e.g. "QWER". When walker == runner, all 4 count == 1. minmum length is updated to 0, and move the walker.
Now walker is past runner, but since walker pointing count == 2 now. It won't update the minimum length. Have to move the runner to minus count.
Time Complexity: O(n). n = s.length.
Space: O(1).
AC Java:
class Solution {
public int balancedString(String s) {
if(s== null || s.length() == 0){
return 0;
} int n = s.length();
int k = n/4;
int [] map = new int[256];
for(char c : s.toCharArray()){
map[c]++;
} int walker = 0;
int runner = 0;
int res = n;
while(runner < n){
map[s.charAt(runner++)]--;
while(walker<n && map['Q']<=k && map['W']<=k && map['R']<=k && map['E']<=k){
res = Math.min(res, runner-walker);
map[s.charAt(walker++)]++;
}
} return res;
}
}
LeetCode 1234. Replace the Substring for Balanced String的更多相关文章
- 【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 i ...
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- JAVA insert() 插入字符串 reverse() 颠倒 delete()和deleteCharAt() 删除字符 replace() 替换 substring() 截取子串
insert() 插入字符串 StringBuffer insert(int index,String str) StringBuffer insert(int index,char ch) Stri ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- 乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters
乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters 一.前言 在算法之中出现最多的就是字符串方面的问题了,关于字符串的 ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
随机推荐
- ubuntu docker 搭建 mongodb,开启授权访问 redis,mysql mssql 备份还原
命令安装docker 如果您想从Ubuntu存储库安装docker版本,则可以运行下面的apt命令. sudo apt install docker.io等到安装完成后,您可以启动Docker并使用s ...
- mysql替换字段中部分字符串
解决:使用replace(obj, search, replace_str)函数; sql语法: UPDATE 表名 SET 字段名=replace(字段名, ‘被替换字符串’, '用来替换的字符串' ...
- centos下 yum快速安装maven
wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos ...
- js-beautify 不换行
最近在用Hbuilder-X,自带js-beautify.但是默认格式化设置实在是看着太难受.来看看一个VX的格式化,参数愣是给用了三行才格式化完,而且两个参数还分开了,看着太难受. // mutat ...
- 读《计算机系统要素:从零开始构建现代计算机》的思考:CodeGen
掌握目标语言的使用.编写 是非常重要的!!! 如果你要实现的Jack语言编译器是把Jack语言代码编译成虚拟机VM代码.或者直接成汇编代码,要完成源代码中unit A——> 目标语言代码转写此u ...
- python爬虫---详解爬虫分类,HTTP和HTTPS的区别,证书加密,反爬机制和反反爬策略,requests模块的使用,常见的问题
python爬虫---详解爬虫分类,HTTP和HTTPS的区别,证书加密,反爬机制和反反爬策略,requests模块的使用,常见的问题 一丶爬虫概述 通过编写程序'模拟浏览器'上网,然后通 ...
- 【转载】C#中ArrayList集合类和List集合类的比较
List集合类和ArrayList集合类都是C#语言中用于存储集合数据的集合类,两者都可灵活的插入.删除以及访问元素等等.但List集合和ArrayList集合的差别还是挺大的,首先List集合类是泛 ...
- 2.JavaScript中的原型规则以及原型设计模式
原型规则 原型规则 所有的引用类型(数组.对象.函数),都具有对象特征,即可自由扩展属性: 所有的引用类型,都有一个_proto_ 属性(隐式原型),属性值是一个普通对象: 所有函数,都具有一个pro ...
- CSS 用法和特性
一.CSS 基本用法 1.CSS 样式语法 样式是 CSS 最小的语法单元,每个样式包含两部分内容:选择器和声明(规则). 语法: p {font-size:12px; color:#333} 注意: ...
- DataPipeline丨「自定义」数据源,解决复杂请求逻辑外部数据获取难题
A公司专注为各种规模和复杂程度的金融投资机构提供一体化投资管理系统,系统主要由投资组合管理.交易执行管理.实时监控管理.风险管理等功能模块构成.随着企业管理产品数量的不断增多,大量数据分散在各券商系统 ...