原题链接在这里: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. EF Core 批处理语句

    在Entity Framework Core (EF Core)有许多新的功能,最令人期待的功能之一就是批处理语句.那么批处理语句是什么呢?批处理语句意味着它不会为每个插入/更新/删除语句发送单独的请 ...

  2. tornado之websoket

    继承WebSoketHandler def open(self): # 当一个WebSoket连接建立之后被调用 def on_message(self, message): # 当客户端发送一个消息 ...

  3. Oralce学习笔记(六)

    逻辑体系结构与Data file 逻辑体系结构关系图 逻辑体系结构最小单位BLOCK的研究 block能装多少行数据: drop table test_block_num purge; create ...

  4. webbrowser实现一个进程一个代理的办法

    public static void RefreshIESettings(string strProxy) { const int INTERNET_OPTION_PROXY = 38; const ...

  5. 两数相加(C#数据结构和算法练习)

    两数相加 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  6. MongoDB netcore

    mongodb.driver mongodb.driver.core url:  http://dl.mongodb.org/dl/win32/x86_64 ********************* ...

  7. WPF调用C++生成的dll文件(示例)

    注:笔者使用的VS版本为2019.1.打开VS2019,选择文件 -> 新建 -> 项目 2.选择项目 新建项目时选择C++“控制台应用”语言:C++平台:Windows项目类型:空项目 ...

  8. IDEA超级实用的几个快捷键

    最近开始使用IDEA,突然发现的比较的实用的几个快捷键 这些快捷键用的好的话真的可以提升很多效率 还有一些比较简单的快捷键,相信大家都会 Ctrl+X:剪切 Ctrl+C:复制 Ctrl+V:粘贴 C ...

  9. pandas-17 关于nan的处理

    pandas-17 关于nan的处理 在pandas中有个另类的存在就是nan,解释是:not a number,不是一个数字,但是它的类型确是一个float类型.numpy中也存在关于nan的方法, ...

  10. Android中自定义环形图2

    如图: 自定义属性,在values文件夹下创建 attrs.xml <?xml version="1.0" encoding="utf-8"?> & ...