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 ...
随机推荐
- python 实例
进度条 import sys, time class ShowProcess(object): """ 显示处理进度的类 调用该类相关函数即可实现处理进度的显示 &quo ...
- DDR3(2):初始化
调取 DDR3 IP核后,是不能直接进行读写测试的,必须先进行初始化操作,对 IP 核进行校验.本篇采用 Modelsim 软件配合 DDR3 IP核生成的仿真模型,搭建出 IP核的初始化过程. 一. ...
- centos6 配置静态IP地址
CentOS网卡配置源文件如下:DEVICE=eth0HWADDR=00:0C:29:A8:67:46TYPE=EthernetUUID=4103d7a8-d073-4e93-ac68-e6f8496 ...
- MySQL之Python与Mysql交互
一:Python操作MySQL步骤 1:Python中操作MySQL的步骤 2.引入模块 在.py文件中引入pymysql模块 import pymysql pymysql是python的一个第三方与 ...
- Beego 学习笔记二:第一个项目
第一个MVC项目 1> 使用beego命令,创建一个项目 首先切换到创建项目的位置,输入bee new firstweb命令,创建成功之后会出现一个名为firstweb的文件夹 2> ...
- 为什么共进电子会选择APS系统?(APS应用案例)
企业介绍 深圳市共进电子股份有限公司(下文简称,共进电子)自1998年创始,以 “发展企业,培养人才,回馈社会”为宗旨,拥有“T&W”商标.2015年2月登陆上交所A股市场,证券代码为6031 ...
- 15 Android系统安全(简要)
Android的用户和第三方软件,Android组件和数据安全 手机root后的问题:1.系统不稳定,2.病毒入侵,3.数据泄露 Root原理: Root分两类:1.临时root,2.永久root R ...
- 非root安装rpm时,mockbuild does not exist - using root
1.现象 [fedora@k8s-cluster--ycmwlao4q5wz-minion- ~]$ [fedora@k8s-cluster--ycmwlao4q5wz-minion- ~]$ sud ...
- NumPy 之 案例(随机漫步)
import numpy as np The numpy.random module supplements(补充) the built-in Python random with functions ...
- 快速了解MongoDB
简介 MongoDB是一款为广泛的现代应用程序设计的高性能.可扩展.分布式数据库系统.MongoDB可用于不同规模大小的组织,为那些对系统低延迟.高吞吐量以及可持续性有很高要求的应用提供稳定关键的服务 ...