Simple Solution Using Sliding Window Technique with Map
Complexity

Time complexity:
word1.length = n, word2.length = m, O(n+m)


Space complexity:
O(n)

Code
# Complexity
- Time complexity:
word1.length = n, word2.length = m, $$O(n + m)$$

- Space complexity:
$$O(n)$$

# Code
```typescript []
function validSubstringCount(word1: string, word2: string): number {
    const m1 = new Map();
    const m2 = new Map();

    for (const c of word2) {
        m2.set(c, (m2.get(c) || 0) + 1);
    }

    let left = 0, matchCount = 0, result = 0;

    for (let right = 0; right < word1.length; right++) {
        const c = word1[right];
        // Add/Update charcter's frequence in map m1
        m1.set(c, (m1.get(c) || 0) + 1);
        // Check if the current character exists in m2 and compare their frequence, to check if we found a match
        if (m1.get(c) === m2.get(c)) {
            matchCount++;
        }       
        // Check if the match count is equal to count of unique characters in word2. Which mean we found a string which contain all the required character of word2 with the required frequence
        // Number of unique characters in word2 = m2 size - 1 (0 index that's why - 1)
        while (matchCount === m2.size) {
            // All char combination which starts from left and ends on(including right as well) and after right will be considered as solutions
            result += word1.length - right;
            // move the window towards left and decrease the freq of character
            const charLeft = word1[left];
            // If this character is part of the m2 then update the match counter and decrease by 1
            if (m1.get(charLeft) === m2.get(charLeft)) {
                matchCount--;
            }
            m1.set(charLeft, m1.get(charLeft) - 1);
            left++;
        }
    }

    return result;
};
```
['TypeScript']