EASY STEPWISE || STANDARD TEMPLATE || WINDOW
\n1. Frequency Arrays:\n   - Two frequency arrays freq1 and freq2 are created to store the frequency of each character in word1 and word2, respectively.\n   - T
\n1. **Frequency Arrays:**\n   - Two frequency arrays `freq1` and `freq2` are created to store the frequency of each character in `word1` and `word2`, respectively.\n   - The frequency of characters in `word2` is initialized.\n\n2. **Sliding Window:**\n   - A sliding window approach is used to iterate through `word1`.\n   - `l` and `r` represent the left and right pointers of the window.\n\n3. **Character Frequency Updates:**\n   - The frequency of the current character (`word1.charAt(r)`) in `freq1` is incremented.\n   - The `flag` variable is set to `true` if the current substring can form `word2`.\n   - The code iterates through the frequency arrays to check if each character in `word1` has a frequency greater than or equal to its corresponding frequency in `word2`. If any character\'s frequency is less, `flag` is set to `false`.\n\n4. **Counting Valid Substrings:**\n   - While `flag` is true and `l` is less than or equal to `r`:\n     - The number of valid substrings (`ans`) is incremented by `m - r` (the length of the current substring).\n     - The frequency of the character at index `l` in `freq1` is decremented.\n     - `l` is incremented.\n     - If the frequency of the current character becomes less than its corresponding frequency in `word2`, `flag` is set to `false`.\n\n5. **Sliding Window and Counting:**\n   - The right pointer `r` is incremented to expand the window.\n   - The process of checking frequencies and counting valid substrings is repeated until `r` reaches the end of `word1`.\n\n6. **Return Result:**\n   - The final `ans` variable holds the total number of valid substrings. The code returns this value.\n\n\n\n\n# Code\n```java []\nclass Solution {\n    public long validSubstringCount(String word1, String word2) {\n        int m=word1.length();\n        int n=word2.length();\n\n        int [] freq1=new int [26];\n        int [] freq2=new int [26];\n\n        for(int i=0;i<n;i++){\n            freq2[word2.charAt(i)-\'a\']++;\n        }\n        long ans=0;\n\n        int l=0;\n        int r=0;\n        while(r<m){\n            freq1[word1.charAt(r)-\'a\']++;\n            boolean flag=true;\n            for(int i=0;i<26;i++){\n                if(freq1[i]<freq2[i]){\n                    flag=false;\n                    break;\n                }\n            }\n            while(flag &&l<=r){\n                ans+=m-r;\n                int alpha=word1.charAt(l)-\'a\';\n                freq1[alpha]--;\n                l++;\n                if(freq1[alpha]<freq2[alpha]){\n                    flag=false;\n                }\n            }\n            r++;\n        }\n        return ans;\n\n\n    }\n}\n```
['Sliding Window', 'Java']