Simple sliding window
Intuition\n Describe your first thoughts on how to solve this problem. \nKeep track of the smallest valid substring.\n\n# Approach\n Describe your approach to s
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nKeep track of the smallest valid substring.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe observe that, for example, with the valid substring "abc" in "deabc".\nthe number of valid strings are:\ndeabc\neabc\nabc\n=> We just find the last position where the substring is valid, and then there will be `lo + 1` valid substrings so far.\n# Complexity\n- Time complexity:$$O(n * 26)$$\n- Space complexity: $$O(n)$$\n\n# Code\n```python3 []\nclass Solution:\n    def validSubstringCount(self, word1: str, word2: str) -> int:\n        count2 = Counter(word2)\n        count1 = Counter()\n        n1, n2 = len(word1), len(word2)\n\n        ans = 0\n\n        lo, hi = 0, 0\n        valid = 0\n\n        while hi < n1:\n            c1 = word1[hi]\n            count1[c1] += 1\n\n            # We only count the first instance when the number of characters in both counts is equal. \n            # so We can avoid duplicate counting.\n            if count1[c1] == count2[c1]:\n                valid += 1\n\n            if valid == len(count2):\n                while lo <= hi:\n                    if count1[word1[lo]] - 1 < count2[word1[lo]]:\n                        break\n                    count1[word1[lo]] -= 1\n                    lo += 1\n                ans += lo + 1\n\n            hi += 1\n\n        return ans\n\n```
['Python3']