3298. 统计重新排列后包含另一个字符串的子字符串数目 II

题目链接 3298. 统计重新排列后包含另一个字符串的子字符串数目 II
思路 滑动窗口
题解链接 O(n) 滑动窗口求个数(Python/Java/C++/Go)
关键点 1. LeetCode-76版本的升级题 2. 内循环中如何统计数量
时间复杂度 O(n+|Σ|)
空间复杂度 O(|Σ|)

代码实现:

class Solution:
def validSubstringCount(self, s: str, t: str) -> int:
if len(s) < len(t):
return 0
cnt = defaultdict(int)
for ch in t:
cnt[ch] += 1
less = len(cnt)
answer = 0
left = 0
for ch in s:
cnt[ch] -= 1
if cnt[ch] == 0:
less -= 1
while less == 0:
if cnt[s[left]] == 0:
less += 1
cnt[s[left]] += 1
left += 1
answer += left
return answer
posted @   WrRan  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示