Sliding Window | O(1) memory
Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(26) = O(1)\n Add your space complexity here, e.g. O(n) 
# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(26) = O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\nwhere \n`n = max(word1.length, word2.length) <= 10^6`\n\n# Code\n```python3 []\nclass Solution:\n    def validSubstringCount(self, word1: str, word2: str) -> int:\n        n = len(word1)\n\n        data = Counter(word2)\n        number_of_chars = len(data)\n        for right, elem in enumerate(word1):\n            if elem not in data:\n                continue\n            data[elem] -= 1\n            if data[elem] == 0:\n                number_of_chars -= 1\n                if number_of_chars == 0:\n                    break\n        \n        if number_of_chars != 0:\n            return 0\n\n        def move_right(right, removed_elem):\n            while right < n:\n                elem = word1[right]\n                right += 1\n                if elem not in data:\n                    continue\n                data[elem] -= 1\n                if elem == removed_elem:\n                    return True, right\n            return False, None\n\n        right += 1\n        result = n - right + 1\n\n        for elem in word1:\n            if elem in data:\n                data[elem] += 1\n                if data[elem] == 1:\n                    is_ok, right = move_right(right, elem)\n                    if not is_ok:\n                        break\n            result += n - right + 1\n\n        return result\n\n```
['Python3']