Sliding Window || O(N)
Approach\nCalculate for each \uD835\uDC56 the number of substrings in word1 that have at least the same character frequency as word2.\ni will contribute to the 
# Approach\nCalculate for each \uD835\uDC56 the number of substrings in word1 that have at least the same character frequency as word2.\n$$i$$ will contribute to the result with the substrings $$0, 1, ... , st$$ where $$st$$ is the last position satisfying the condition $$freq[i] - freq[st] >= freqWord2$$\n\n# Complexity\n- Time complexity:$$O(n * 26)$$ => $$O(n)$$, where n is the number of characters in word1\n- Space complexity: $$O(2 * 26)$$ => $$O(1)$$\n\nSame [solution](https://leetcode.com/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-ii/solutions/5822560/sliding-window/) for the Hard problem [https://leetcode.com/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-ii](https://leetcode.com/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-ii)\n\uD83D\uDCAA\uD83D\uDE80Keep leetcoding! Keep improving!\n# Code\n```cpp []\nclass Solution {\npublic:\n    long long validSubstringCount(string word1, string word2) {\n        int freqWord2[26] = {}, freq[26] = {};\n        int w2n = word2.size(), w1n = word1.size();\n        for (int i = 0; i < w2n; i++) \n        {\n            freqWord2[word2[i] - \'a\']++;\n        }\n\n        int st = 0;\n        long long result = 0;\n        bool isBiggerOnce = false;\n        for(int i = 0; i < w1n; i++) \n        {\n            freq[(word1[i] - \'a\')]++;\n            int ch = word1[st] - \'a\';\n            while(st <= i && freq[ch] - 1 >= freqWord2[ch]) \n            {\n                freq[ch]--;\n                st++;\n                ch = word1[st] - \'a\';     \n            }\n            //above is maintained the frequency bigger or equal so we retain if once was bigger for word2 characters.\n            if (isBiggerOnce || IsBiggerOrEqual(freq, freqWord2)) { \n                isBiggerOnce = true; \n                result += (long long)(st + 1);\n            }  \n        }\n\n        return result;\n    }\n    bool IsBiggerOrEqual(int freq1[], int freq2[]) \n    {\n        for (int i = 0; i < 26; i++) \n        {\n            if (freq1[i] < freq2[i]) \n                return false;\n        }\n        return true;\n    }\n};\n```
['C++']