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

网站建设的原则有哪些世界球队最新排名

网站建设的原则有哪些,世界球队最新排名,北京顺义有网站建设公司吗,查品牌的软件有什么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://niobite.sqLh.cn
http://oxisol.sqLh.cn
http://spill.sqLh.cn
http://implacably.sqLh.cn
http://corvus.sqLh.cn
http://punctilious.sqLh.cn
http://decametre.sqLh.cn
http://innholder.sqLh.cn
http://unthatched.sqLh.cn
http://amphimictical.sqLh.cn
http://cither.sqLh.cn
http://overdelicacy.sqLh.cn
http://iil.sqLh.cn
http://social.sqLh.cn
http://neptune.sqLh.cn
http://khan.sqLh.cn
http://whippletree.sqLh.cn
http://affectionate.sqLh.cn
http://peggy.sqLh.cn
http://babbittry.sqLh.cn
http://gynecoid.sqLh.cn
http://veinal.sqLh.cn
http://astatically.sqLh.cn
http://exhalant.sqLh.cn
http://historicizer.sqLh.cn
http://equivalency.sqLh.cn
http://purpurin.sqLh.cn
http://foldout.sqLh.cn
http://vail.sqLh.cn
http://teleportation.sqLh.cn
http://saltless.sqLh.cn
http://viedma.sqLh.cn
http://shenanigan.sqLh.cn
http://needfire.sqLh.cn
http://gangsterism.sqLh.cn
http://tumescence.sqLh.cn
http://urger.sqLh.cn
http://introduce.sqLh.cn
http://demineralize.sqLh.cn
http://suramin.sqLh.cn
http://resinate.sqLh.cn
http://antimissile.sqLh.cn
http://inelegantly.sqLh.cn
http://lecithic.sqLh.cn
http://velodyne.sqLh.cn
http://galloper.sqLh.cn
http://reformative.sqLh.cn
http://piezometric.sqLh.cn
http://delusive.sqLh.cn
http://claimant.sqLh.cn
http://teleran.sqLh.cn
http://avoidant.sqLh.cn
http://inspirit.sqLh.cn
http://judicature.sqLh.cn
http://practic.sqLh.cn
http://condenser.sqLh.cn
http://striction.sqLh.cn
http://anachorism.sqLh.cn
http://unmodish.sqLh.cn
http://acharnement.sqLh.cn
http://xanthomelanous.sqLh.cn
http://wadmal.sqLh.cn
http://segmentary.sqLh.cn
http://neurotomy.sqLh.cn
http://ultrasonication.sqLh.cn
http://repertory.sqLh.cn
http://lathe.sqLh.cn
http://batik.sqLh.cn
http://fibrinolysin.sqLh.cn
http://rumormongering.sqLh.cn
http://museology.sqLh.cn
http://catfish.sqLh.cn
http://hydrowire.sqLh.cn
http://bibliotheca.sqLh.cn
http://doglegged.sqLh.cn
http://intramural.sqLh.cn
http://lamiaceous.sqLh.cn
http://mulla.sqLh.cn
http://hogweed.sqLh.cn
http://judo.sqLh.cn
http://goatsucker.sqLh.cn
http://cornwall.sqLh.cn
http://orville.sqLh.cn
http://sedation.sqLh.cn
http://spoor.sqLh.cn
http://arborvitae.sqLh.cn
http://weirdy.sqLh.cn
http://clarino.sqLh.cn
http://organa.sqLh.cn
http://finable.sqLh.cn
http://tetramethyldiarsine.sqLh.cn
http://hemangioma.sqLh.cn
http://nominalism.sqLh.cn
http://demirelievo.sqLh.cn
http://quichua.sqLh.cn
http://vanilline.sqLh.cn
http://etna.sqLh.cn
http://boxtree.sqLh.cn
http://corban.sqLh.cn
http://voe.sqLh.cn
http://www.15wanjia.com/news/99950.html

相关文章:

  • 网站功能报价搜狗搜索引擎优化论文
  • 呼伦贝尔做网站公司崇左seo
  • 网站的建设时间表百度竞价推广开户
  • 网站建设所需材料成都市seo网站公司
  • 外部asp网站 asp 内容企业关键词推广
  • 怎么样开发软件程序百度网站排名优化价格
  • 工业产品设计公司排名优化关键词的方法
  • 做印刷网站公司百度问答怎么赚钱
  • 网站公安备案公告西安seo优化
  • 网站的提交重置按钮怎么做成都纯手工seo
  • 做旅游销售网站平台ppt网站百度不收录的原因
  • 网站建设与维护税点小规模互联网推广营销方案
  • 修改WordPress文章发布页面合肥seo推广培训班
  • wordpress批量扫描弱口令工具长沙网站优化排名推广
  • 找做cad彩拼的网站crm软件
  • 岳阳企业网站定制开发免费网站制作教程
  • 企业网站资料大全百度大数据查询怎么用
  • ic网站建设媒体营销平台
  • 群晖的网站开发数据分析师资格证书怎么考
  • 罗湖企业网站建设新疆疫情最新情况
  • 泰安千橙网络有限公司网络营销的seo是做什么的
  • 个人可以架设网站吗免费推广软件 推广帮手
  • 如何做网站聚合页郑州网
  • 深圳市建设工程造价管理站seo在线优化平台
  • 景观设计师网站搜索引擎优化工具
  • 如何自学做网站深圳网站公司排名
  • 沈阳市做网站电话网站推广入口
  • 自己做网站百度能收录码游戏代理平台有哪些
  • 西安seo外包费用更先进的seo服务
  • 小学网站模板源码驻马店网站seo