Best Clean Cool Solution O(n) time O(1) space
IntuitionWe use one array of size 26 to differentiate how many instances of a certain letter we need and how many we have (we could use one variable to keep tha
# Intuition
We use one array of size 26 to differentiate how many instances of a certain letter we need and how many we have (we could use one variable to keep that score, but anyways, we have two for clarity)
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
Code is readable
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
/**
 * @param {string} word1
 * @param {string} word2
 * @return {number}
 */
var validSubstringCount = function(word1, word2) {
    let res = 0;
    let freq = new Array(26).fill(0);
    let left = 0;
    let allScore = 0;
    let curScore = 0;
    for(let i = 0; i < word2.length; i++){
        if(freq[word2.charCodeAt(i) - 97] == 0) allScore++;
        freq[word2.charCodeAt(i) - 97]--;
    }
    for(let i = 0; i < word1.length; i++){
        if(freq[word1.charCodeAt(i) - 97] == -1) curScore++;
        freq[word1.charCodeAt(i) - 97]++;
        while(allScore == curScore){
            res += word1.length - i;
            freq[word1.charCodeAt(left) - 97]--;
            if(freq[word1.charCodeAt(left) - 97] == -1) curScore--;
            left++;
        }
    }
    return res;
};
```
['Hash Table', 'String', 'Sliding Window', 'JavaScript']