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

题目链接 3297. 统计重新排列后包含另一个字符串的子字符串数目 I
思路 滑动窗口
题解链接 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  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
点击右上角即可分享
微信分享提示