跳到主要内容

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

链接:3297.统计重新排列后包含另一个字符串的子字符串数目I
难度:Medium
标签:哈希表、字符串、滑动窗口
简介:请你返回 word1 中 合法 子字符串的数目。

题解 1 - python

  • 编辑时间:2025-01-09
  • 执行用时:303ms
  • 内存消耗:17.91MB
  • 编程语言:python
  • 解法介绍:遍历
class Solution:
def validSubstringCount(self, word1: str, word2: str) -> int:
c = Counter(word2)
f = len(c)
res = l = 0
for r in range(len(word1)):
c[word1[r]] -= 1
if c[word1[r]] == 0: f -= 1
while l < r and c[word1[l]] < 0:
c[word1[l]] += 1
if c[word1[l]] == 1: f += 1
l += 1
if f == 0: res += l + 1
return res