Alternate simple impl of sliding window. with logic PseudoCode
\n//we store freq of word2. \n//for a substring of word1, if same freq matched then \n// we can make a prefix out of it by rearranging them.\n//so goal becomes 
```\n//we store freq of word2. \n//for a substring of word1, if same freq matched then \n// we can make a prefix out of it by rearranging them.\n//so goal becomes to see if substring has enough characters to make word2 prefix.\n\n//for a window, we have two choices: add char to window or remove char from window\n//if k is count of characters remaining to match then\n\n//if k> 0 then word2 is yet to be matched\n//  ADD CHAR\n//if k==0 then word2 has been matched.\n//  res = wor1.length - endindexOfWindow\n//  REMOVE CHAR\n//  we repeat this until we are in deficit of reqFreq.\n\n//=======\n//ADD CHAR logic: \n// freq++ for that char.\n// if freq <= reqFreq for that char\n//   k-- as we got one req freq. \n// if freq > reqfreq for that char\n//   we added extra freq of this char. no change in k\n//-------\n//REMOVE CHAR logic\n//  freq-- for that char\n//  if freq < reqFreq for that char\n//      k++ as we now need to fill this requirement\n//  if freq >= reqFreq\n//      we have extra. no change in k.\n//========\n\n//Execution stops when window reaches end.\n```\n\n# Code\n```javascript []\n/**\n * @param {string} word1\n * @param {string} word2\n * @return {number}\n */\nvar validSubstringCount = function(word1, word2) { \n    let word1freq = [],word2freq = [], k = 0;\n    for(let i=0; i<26; i++) {\n        word2freq.push(0);\n        word1freq.push(0);\n    }\n    function getcharindex(char) {\n        let str = \'abcdefghijklmnopqrstuvwxyz\';\n        return str.indexOf(char);\n    }\n    for(let i=0; i<word2.length; i++) {\n        word2freq[getcharindex(word2[i])]++;\n    }\n    k = word2.length;\n    let start = 0, end = 0, res = 0;\n    while(end < word1.length) {\n        if (k > 0) {\n            let charindex = getcharindex(word1[end]);\n            end++;\n            word1freq[charindex]++;\n            if(word1freq[charindex] <= word2freq[charindex]) {\n                k--;\n            } else {\n                //do nothing\n            }\n        } \n        while(k == 0) {\n            //word2 characters matched\n            res += word1.length - (end - 1 );\n            let charindex = getcharindex(word1[start]);\n            start++;\n            word1freq[charindex]--;\n            if(word1freq[charindex] < word2freq[charindex]) {\n                k++;\n            } else {\n                //do nothing\n            }\n        } \n    }\n    return res;\n};\n```
['JavaScript']