classSolution: defvalidSubstringCount(self, word1: str, word2: str) -> int: ans = 0 b_cnt = defaultdict(int) for s in word2: b_cnt[s] += 1 n = len(word1) a_cnt = defaultdict(int) tmp = 0 st = 0 for i inrange(0, n): if tmp == len(word2): break st = i a_cnt[word1[i]] += 1
if a_cnt[word1[i]] == b_cnt[word1[i]]: tmp += b_cnt[word1[i]] if tmp != len(word2): return0 ans = 0
tt = 0 while a_cnt[word1[tt]] - 1 >= b_cnt[word1[tt]]: a_cnt[word1[tt]] -= 1 tt += 1 ans += tt + 1
for i inrange(st+1, n): # print(i, ans, tt)
a_cnt[word1[i]] += 1 while a_cnt[word1[tt]] - 1 >= b_cnt[word1[tt]]: a_cnt[word1[tt]] -= 1 tt += 1 # a_cnt[word1[i]] += 1 ans += tt + 1 return ans