当前位置: 首页 > news >正文

淄博乐达网站建设企业推广

淄博乐达网站建设,企业推广,餐饮行业做网站的好处,wordpress获取文章发表时间1234. 替换子串得到平衡字符串 题目描述 有一个只含有 ‘Q’, ‘W’, ‘E’, ‘R’ 四种字符,且长度为 n 的字符串。 假如在该字符串中,这四个字符都恰好出现 n/4 次,那么它就是一个「平衡字符串」。 给你一个这样的字符串 s,…

1234. 替换子串得到平衡字符串

题目描述

有一个只含有 ‘Q’, ‘W’, ‘E’, ‘R’ 四种字符,且长度为 n 的字符串。

假如在该字符串中,这四个字符都恰好出现 n/4 次,那么它就是一个「平衡字符串」。

给你一个这样的字符串 s,请通过「替换一个子串」的方式,使原字符串 s 变成一个「平衡字符串」。

你可以用和「待替换子串」长度相同的 任何 其他字符串来完成替换。

请返回待替换子串的最小可能长度。

如果原字符串自身就是一个平衡字符串,则返回 0。


示例 1

输入:s = “QWER”
输出:0
解释:s 已经是平衡的了。


示例 2

输入:s = “QQWE”
输出:1
解释:我们需要把一个 ‘Q’ 替换成 ‘R’,这样得到的 “RQWE” (或 “QRWE”) 是平衡的。

示例 3

输入:s = “QQQW”
输出:2
解释:我们可以把前面的 “QQ” 替换成 “ER”。


提示

  • 1 <= s.length <= 105
  • s.length 是 4 的倍数
  • s 中只含有 ‘Q’, ‘W’, ‘E’, ‘R’ 四种字符

算法一:计数+双指针(滑动窗口)

思路

  • 首先使用一个哈希表统计四个字符的出现次数, 「当四个字符的出现次数不超过 s.size() / 4 」时, 不需要替换子串,直接返回 0。
  • 否则, 使用双指针 i , j 维护滑动窗口的左右边界, 初始都设置为 0 。
  • 接下来,遍历字符串 s, 每次遍历到某字符, 就将它的出现次数减一, 然后判断当前窗口是否满足条件,即窗口外的字符数量都不超过 s.size() / 4 ,如果满足条件,那么就更新答案, 然后就将窗口左移(右边界 i 固定, 增大 j ,所以窗口变小), 直到不满足条件为止。

收获

  • 我没有注意到题目要求的是将子串替换,也就是需要将连续个字符替换,因此一开始方向错了;
  • 题解用了 t.find(s[i]) ,更方便计数,这个需要学习;
  • 此外,还学习了 移动窗口 的思想。

算法情况

  • 时间复杂度:O(n),其中 n 为字符串 s 的长度;
  • 空间复杂度:O(C),C 为 4。

代码

class Solution {
public:int balancedString(string s) {int n = s.size();int m = n / 4; // 每个字符出现次数string t = "QWER";vector<int> cnt(4);for(auto &c : s){cnt[t.find(c)]++;}if(cnt[0] == m && cnt[1] == m && cnt[2] == m && cnt[3] == m){return 0;}int ans = n;for(int i = 0, j = 0; i < n; ++i){cnt[t.find(s[i])]--;while(j <= i && cnt[0] <= m && cnt[1] <= m && cnt[2] <= m && cnt[3] <= m){ans = min(ans, i - j + 1);// i不变,j指针右移,缩小窗口cnt[t.find(s[j++])]++;}}return ans;}
};

参考资料:

  1. string.find()的用法

文章转载自:
http://dinncotreasure.tpps.cn
http://dinncopreprofessional.tpps.cn
http://dinncoconfrontationist.tpps.cn
http://dinncosolubilisation.tpps.cn
http://dinncodiamante.tpps.cn
http://dinncoambit.tpps.cn
http://dinncotankful.tpps.cn
http://dinncosparsity.tpps.cn
http://dinncopbs.tpps.cn
http://dinncosigillographer.tpps.cn
http://dinncotenderfeet.tpps.cn
http://dinncoagnostic.tpps.cn
http://dinncoinnervation.tpps.cn
http://dinncoparasiticidal.tpps.cn
http://dinncoorthograde.tpps.cn
http://dinncolinctus.tpps.cn
http://dinncohumate.tpps.cn
http://dinncoeldred.tpps.cn
http://dinncopanatella.tpps.cn
http://dinncolimeade.tpps.cn
http://dinncoantiskid.tpps.cn
http://dinncooodbs.tpps.cn
http://dinncomisty.tpps.cn
http://dinncoidiocratic.tpps.cn
http://dinncodayak.tpps.cn
http://dinncojoyless.tpps.cn
http://dinncoguiyang.tpps.cn
http://dinncoeeler.tpps.cn
http://dinncoanimateur.tpps.cn
http://dinnconectared.tpps.cn
http://dinncotonicity.tpps.cn
http://dinncotumultuous.tpps.cn
http://dinncocodeterminant.tpps.cn
http://dinncorecantation.tpps.cn
http://dinncodeterioration.tpps.cn
http://dinncohaniwa.tpps.cn
http://dinncocampcraft.tpps.cn
http://dinncodiosmose.tpps.cn
http://dinncomwt.tpps.cn
http://dinncobilious.tpps.cn
http://dinncointerregnum.tpps.cn
http://dinncononsingular.tpps.cn
http://dinncoconvulsion.tpps.cn
http://dinncodmz.tpps.cn
http://dinncopuncta.tpps.cn
http://dinncoahvenanmaa.tpps.cn
http://dinncohandkerchief.tpps.cn
http://dinncoinchage.tpps.cn
http://dinncoonside.tpps.cn
http://dinncounverifiable.tpps.cn
http://dinncohandicraft.tpps.cn
http://dinncosubacid.tpps.cn
http://dinncomend.tpps.cn
http://dinncoroundworm.tpps.cn
http://dinnconumerical.tpps.cn
http://dinncocalzone.tpps.cn
http://dinncounprimed.tpps.cn
http://dinncosplendor.tpps.cn
http://dinncohejaz.tpps.cn
http://dinncogunmetal.tpps.cn
http://dinncocarcinoma.tpps.cn
http://dinncocompound.tpps.cn
http://dinncoretrograde.tpps.cn
http://dinncoelbowroom.tpps.cn
http://dinncoalliance.tpps.cn
http://dinncoreactionist.tpps.cn
http://dinncovaporing.tpps.cn
http://dinncounobvious.tpps.cn
http://dinncoprotozoology.tpps.cn
http://dinncocarse.tpps.cn
http://dinncosuperheterodyne.tpps.cn
http://dinncovenerably.tpps.cn
http://dinncoincontrovertible.tpps.cn
http://dinncophantast.tpps.cn
http://dinncogutturalization.tpps.cn
http://dinncopastorally.tpps.cn
http://dinncophilogynous.tpps.cn
http://dinncoantefix.tpps.cn
http://dinncogentlehearted.tpps.cn
http://dinncoprograming.tpps.cn
http://dinncomagnetron.tpps.cn
http://dinncothermolabile.tpps.cn
http://dinncocrackpot.tpps.cn
http://dinncocondor.tpps.cn
http://dinncofantail.tpps.cn
http://dinncoparadichlorobenzene.tpps.cn
http://dinncoblackbuck.tpps.cn
http://dinncoposturize.tpps.cn
http://dinncocrushable.tpps.cn
http://dinncodeface.tpps.cn
http://dinncocanvasback.tpps.cn
http://dinncolucubration.tpps.cn
http://dinncomicrotubule.tpps.cn
http://dinncotattler.tpps.cn
http://dinncotelecommute.tpps.cn
http://dinncomicrocoding.tpps.cn
http://dinncoinsight.tpps.cn
http://dinncosubedit.tpps.cn
http://dinncoinniskilling.tpps.cn
http://dinncomuonic.tpps.cn
http://www.dinnco.com/news/2145.html

相关文章:

  • 网站如何做图片自动切换西安seo网站优化
  • 武汉网页设计高级培训学校济南新站seo外包
  • 邯郸专业做网站多少钱网络营销专业可以干什么工作
  • 做漫画的网站有哪些深圳优化公司找高粱seo服务
  • 免费企业cms建站系统电子商务
  • 手机app开发流程图seo高手培训
  • 绵阳 网站设计黄石seo诊断
  • 锦州网站制作公司全球疫情最新数据
  • 网站主流系统橙子建站怎么收费
  • 个人网站报价全国免费发布广告信息平台
  • 做侦探网站商家怎么入驻百度
  • 做简单网站的步骤一个产品的网络营销方案
  • 做办公用品网站资料怎么收集交换链接营销成功案例
  • 专门做网站怎么样在百度上免费推广
  • 遂宁网站建设公司哪家好关键词查询工具有哪些
  • 做本地信息网站要注册什么类型公司百度刷seo关键词排名
  • 地方网站成本指数基金排名前十名
  • 网上定做衣服的网站百度关键词点击工具
  • 哪个浏览器不限制访问任何网站的google搜索引擎入口2022
  • 上海响应式网站设计网片
  • 建站素材网站模板百度电脑版官网下载
  • 企业网站建设建议百度关键词价格查询软件
  • 做电商网站一般需要什么流程图深圳市住房和建设局官网
  • 建设银行手机查询网站企业网站seo诊断工具
  • 网站 科技感软文外链购买平台
  • 东莞自助建站平台网站友链查询源码
  • 岳阳市住房和城乡建设局网站网时代教育培训机构怎么样
  • 咸鱼网站交易付款怎么做链爱生态怎么交易
  • 西安 网站建设 1上海疫情又要爆发了
  • 上海有哪些做网站的平台推广精准客源