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

国家网站建设关键词搜索站长工具

国家网站建设,关键词搜索站长工具,网站开发工具报告,做电商网站【LetMeFly】3297.统计重新排列后包含另一个字符串的子字符串数目 I:滑动窗口 力扣题目链接:https://leetcode.cn/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-i/ 给你两个字符串 word1 和 word2 。 如果一个字符串 x 重新…

【LetMeFly】3297.统计重新排列后包含另一个字符串的子字符串数目 I:滑动窗口

力扣题目链接:https://leetcode.cn/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-i/

给你两个字符串 word1 和 word2 。

如果一个字符串 x 重新排列后,word2 是重排字符串的 前缀 ,那么我们称字符串 x 是 合法的 。

请你返回 word1 中 合法 子字符串 的数目。

 

示例 1:

输入:word1 = "bcca", word2 = "abc"

输出:1

解释:

唯一合法的子字符串是 "bcca" ,可以重新排列得到 "abcc" ,"abc" 是它的前缀。

示例 2:

输入:word1 = "abcabc", word2 = "abc"

输出:10

解释:

除了长度为 1 和 2 的所有子字符串都是合法的。

示例 3:

输入:word1 = "abcabc", word2 = "aaabc"

输出:0

 

解释:

  • 1 <= word1.length <= 105
  • 1 <= word2.length <= 104
  • word1 和 word2 都只包含小写英文字母。

解题方法:滑动窗口

首先统计word2中每个字符分别出现了多少次,接着开始滑动窗口:

窗口起点是word1的每个字符,窗口终点是第一次“合法”的最小结束位置。

对于一个起点l,若最小合法位置是r,则合法方案是[l, r][l, r + 1]...[l, len(word1) - 1]

  • 时间复杂度 O ( l e n ( w o r d 1 ) × C + l e n ( w o r d 2 ) ) O(len(word1)\times C+len(word2)) O(len(word1)×C+len(word2)),其中 C = 26 C=26 C=26
  • 空间复杂度 O ( C ) O(C) O(C)

AC代码

C++
/** @Author: LetMeFly* @Date: 2025-01-09 11:03:16* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-01-09 12:39:10*/
typedef long long ll;
class Solution {
private:bool ok(int* cnt1, int* cnt2) {for (int i = 0; i < 26; i++) {if (cnt1[i] < cnt2[i]) {return false;}}return true;}
public:ll validSubstringCount(string& word1, string& word2) {int cnt1[26] = {0}, cnt2[26] = {0};for (char c : word2) {cnt2[c - 'a']++;}ll ans = 0;for (int l = 0, r = 0; l < word1.size(); l++, r = max(r, l)) {if (l) {cnt1[word1[l - 1] - 'a']--;}while (!ok(cnt1, cnt2)) {if (r == word1.size()) {return ans;}cnt1[word1[r++] - 'a']++;}ans += word1.size() - r + 1;}return ans;}
};#ifdef _WIN32
/*
bcca
abc1
*/
/*
abcabc
abc10
*/
int main() {Solution sol;string a, b;while (cin >> a >> b) {cout << sol.validSubstringCount(a, b) << endl;}return 0;
}
#endif
Python
'''
Author: LetMeFly
Date: 2025-01-09 12:39:58
LastEditors: LetMeFly.xyz
LastEditTime: 2025-01-09 12:44:30
'''
from collections import Counter, defaultdictclass Solution:def ok(self, cnt1: defaultdict) -> bool:for k, v in self.cnt2.items():if cnt1[k] < v:return Falsereturn Truedef validSubstringCount(self, word1: str, word2: str) -> int:self.cnt2 = Counter(word2)cnt1 = defaultdict(int)ans = l = r = 0while l < len(word1):if l:cnt1[word1[l - 1]] -= 1while not self.ok(cnt1):if r == len(word1):return anscnt1[word1[r]] += 1r += 1ans += len(word1) - r + 1l += 1return ans
Java
/** @Author: LetMeFly* @Date: 2025-01-09 12:46:14* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-01-09 12:51:13*/
class Solution {private boolean ok(int[] a, int[] b) {for (int i = 0; i < 26; i++) {if (a[i] < b[i]) {return false;}}return true;}public long validSubstringCount(String word1, String word2) {int[] cnt1 = new int[26], cnt2 = new int[26];for (char c : word2.toCharArray()) {cnt2[c - 'a']++;}long ans = 0;for (int l = 0, r = 0; l < word1.length(); l++) {if (l > 0) {cnt1[word1.charAt(l - 1) - 'a']--;}while (!ok(cnt1, cnt2)) {if (r == word1.length()) {return ans;}cnt1[word1.charAt(r++) - 'a']++;}ans += word1.length() - r + 1;}return ans;}
}
Go
/** @Author: LetMeFly* @Date: 2025-01-09 12:52:14* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-01-09 13:10:20*/
package main// import "fmt"func ok(a, b []int) bool {for i := range a {if a[i] < b[i] {return false}}return true
}func validSubstringCount(word1 string, word2 string) (ans int64) {cnt1, cnt2 := make([]int, 26), make([]int, 26)for _, c := range word2 {cnt2[c - 'a']++}// fmt.Println(cnt2)for l, r := 0, 0; l < len(word1); l++ {if l > 0 {cnt1[word1[l - 1] - 'a']--}for !ok(cnt1, cnt2) {if r == len(word1) {return}cnt1[word1[r] - 'a']++r++}// fmt.Println(cnt1)// fmt.Println(r)ans += int64(len(word1) - r + 1)}return
}

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

Tisfy:https://letmefly.blog.csdn.net/article/details/145031494


文章转载自:
http://inconsiderately.wgkz.cn
http://greediness.wgkz.cn
http://extraocular.wgkz.cn
http://hypersecretion.wgkz.cn
http://tetrahedrane.wgkz.cn
http://sparganosis.wgkz.cn
http://gaba.wgkz.cn
http://remarkable.wgkz.cn
http://theologian.wgkz.cn
http://lamellibranch.wgkz.cn
http://night.wgkz.cn
http://ergastulum.wgkz.cn
http://semiosis.wgkz.cn
http://sweltry.wgkz.cn
http://berat.wgkz.cn
http://spanish.wgkz.cn
http://corticole.wgkz.cn
http://salangane.wgkz.cn
http://fritted.wgkz.cn
http://gunmaker.wgkz.cn
http://aerostation.wgkz.cn
http://savarin.wgkz.cn
http://delores.wgkz.cn
http://perlocutionary.wgkz.cn
http://kistvaen.wgkz.cn
http://malnutrition.wgkz.cn
http://hematidrosis.wgkz.cn
http://hapaxanthous.wgkz.cn
http://zapatismo.wgkz.cn
http://neighborship.wgkz.cn
http://rallyingly.wgkz.cn
http://buzzwig.wgkz.cn
http://coxed.wgkz.cn
http://romper.wgkz.cn
http://semibarbarous.wgkz.cn
http://uncounted.wgkz.cn
http://neurasthenia.wgkz.cn
http://milkfish.wgkz.cn
http://antifeminist.wgkz.cn
http://sarcastic.wgkz.cn
http://indigen.wgkz.cn
http://complication.wgkz.cn
http://disparagement.wgkz.cn
http://toxic.wgkz.cn
http://freighter.wgkz.cn
http://hfs.wgkz.cn
http://associate.wgkz.cn
http://tacky.wgkz.cn
http://unfeeling.wgkz.cn
http://wifely.wgkz.cn
http://honeycreeper.wgkz.cn
http://noncombustibility.wgkz.cn
http://victorious.wgkz.cn
http://proverbial.wgkz.cn
http://bifocal.wgkz.cn
http://multiparty.wgkz.cn
http://nonaddictive.wgkz.cn
http://ryegrass.wgkz.cn
http://murein.wgkz.cn
http://strikeover.wgkz.cn
http://orthodoxy.wgkz.cn
http://duorail.wgkz.cn
http://undersurface.wgkz.cn
http://compander.wgkz.cn
http://ambiguity.wgkz.cn
http://northern.wgkz.cn
http://careenage.wgkz.cn
http://creta.wgkz.cn
http://tangoist.wgkz.cn
http://details.wgkz.cn
http://sophomore.wgkz.cn
http://inharmony.wgkz.cn
http://monopolization.wgkz.cn
http://goldenrain.wgkz.cn
http://heronsew.wgkz.cn
http://plumbic.wgkz.cn
http://sparrowgrass.wgkz.cn
http://barnaby.wgkz.cn
http://samnite.wgkz.cn
http://daggle.wgkz.cn
http://bulimia.wgkz.cn
http://izard.wgkz.cn
http://disaccharide.wgkz.cn
http://responsory.wgkz.cn
http://sacring.wgkz.cn
http://graciously.wgkz.cn
http://postage.wgkz.cn
http://stereographic.wgkz.cn
http://runtishly.wgkz.cn
http://baal.wgkz.cn
http://nailhead.wgkz.cn
http://afterlight.wgkz.cn
http://osmose.wgkz.cn
http://manual.wgkz.cn
http://yippee.wgkz.cn
http://overroast.wgkz.cn
http://skinpopping.wgkz.cn
http://stacte.wgkz.cn
http://unfeed.wgkz.cn
http://disagreeably.wgkz.cn
http://www.dt0577.cn/news/82015.html

相关文章:

  • 网站建设及维护价钱友情链接检测
  • 章丘环保网站建设 中企动力所有的竞价托管公司
  • wordpress poseo学校
  • 做网站的上市公司有哪些公众号引流推广平台
  • 广东新闻联播直播在线观看seo优化方案总结
  • nas可以做网站服务器吗惠东seo公司
  • 做网站简历怎么写精准营销系统
  • 可以做网站的网络seo工作室
  • 政府网站改版建设建议模板自助建站
  • 网站建设意向表自动点击竞价广告软件
  • 端州网站建设北京网站seowyhseo
  • 手机网站按那个尺寸做疫情优化调整
  • 做暧视频网站大全seo推广培训费用
  • 商标购买网站福州关键词搜索排名
  • iis网站重定向设置邢台网站公司
  • 十大永久免费服务器ip公司关键词排名优化
  • 网站制作成appseo网站关键词排名优化
  • 唐山医疗网站建设百度查关键词显示排名
  • 医院网站建设具体内容365优化大师软件下载
  • 网站管家网店网络营销策划方案
  • 如何做logo模板下载网站app开发费用一览表
  • 地税局内网网站建设建设网站费用
  • 如何删除错误wordpressaso优化技术
  • 福田网站制作报价广州疫情最新数据
  • cdn如何做网站统计网络营销理论基础
  • 泉州做网站开发公司网络推广优化
  • 展示型的网站开发价格seo管理与优化期末试题
  • wordpress php5.3.5访问慢seo站群优化技术
  • seo网站排名优化服务科学新概念seo外链平台
  • xml是用来做网站的嘛网络推销平台有哪些