原题链接在这里: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 of 4
  • 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;
}
}

类似Minimum Window Substring.

LeetCode 1234. Replace the Substring for Balanced String的更多相关文章

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

  2. LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

    LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...

  3. JAVA insert() 插入字符串 reverse() 颠倒 delete()和deleteCharAt() 删除字符 replace() 替换 substring() 截取子串

    insert() 插入字符串 StringBuffer insert(int index,String str) StringBuffer insert(int index,char ch) Stri ...

  4. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  5. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  6. 乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters

    乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters 一.前言 在算法之中出现最多的就是字符串方面的问题了,关于字符串的 ...

  7. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  8. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

  9. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

随机推荐

  1. 迭代子(Iterator)模式

    迭代子模式又叫做游标模式.迭代子模式可以顺序地访问一个聚集中的元素而必暴露聚集的内部表象. 1.  聚集和Java聚集 多个对象在一起形成的总体形成聚集(Aggregate),聚集对象是能够包容一组对 ...

  2. 如何解决Requests的SSLError(转)

    add by zhj: 我使用方法2“更新系统的certificate”解决了问题 原文:https://www.jianshu.com/p/8deb13738d2c 这两天在Linux上爬Googl ...

  3. JavaScript入门(二)

    JavaScript入门—操作DOM树 要点 DOM树是一个树形结构,操作DOM树通常是“更新.遍历.新增.删除”. 更新DOM树 拿到DOM节点 var id=document.getElement ...

  4. 阿里sentinel源码研究深入

    1. 阿里sentinel源码研究深入 1.1. 前言 昨天已经把sentinel成功部署到线上环境,可参考我上篇博文,该走的坑也都走了一遍,已经可以初步使用它的限流和降级功能,根据我目前的实践,限流 ...

  5. css中absolute设置问题和如何让div居中

    今天设置多个div到页面正中间的时候,在第一层<div class="map">中设置如下: .map{ position:absolute: top:50%; lef ...

  6. QTGraphics-View拖拽以及鼠标指针操作

    因为QGraphicsView继承自QWidget,它也提供了像QWidget那样的拖拽功能. 另外,为了方便,Graphics View框架也为场景以及每个item提供拖拽支持.当视图接收到拖拽事件 ...

  7. android中activity和service是否在同一个进程中

    分两种情况,如果是本地线程,肯定是同一个进程中的, 如果是远程服务,那么activity和service将在不同的进程中的 ----- 非远程服务,和Activity属于同一个进程和线程:而远程服务和 ...

  8. docker中安装及使用mysql

    打算构造一个环境较全的linux环境,所以在本地弄了个docker.然后pull了一个centos的镜像,并打算在此镜像的基本上,构建适合自己的镜像.但在使用时,发现了各种问题,还是费了一些功夫.原因 ...

  9. python之路第五天

    字符串的应用(二) expandtabs 断句16,不够16个,用空格补齐 s = "username\te-mail\tpassword\nxiaoming\t123@qq.com\t12 ...

  10. pip问题:ImportError: cannot import name main

    问题描述 今天使用pip安装python包的时候,提示可以升级到最新版的pip,然后就升级了pip,从8.1.1到19.0.3,结果,就出现了下面的问题,pip不能用了: Traceback (mos ...