Short Clean and Concise || Java || Sliding Window || Beats 80% ||
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public long validSubstringCount(String word1, String word2) {\n        final int n = word1.length();\n        final int m = word2.length();\n        final int[] freq1 = new int[26];\n        final int[] freq2 = new int[26];\n\n        for (int i = 0; i < m; i++) {\n            freq1[word2.charAt(i) - \'a\']++;\n        }\n\n        int j = 0, count = 0;\n        long ans = 0;\n\n        for (int i = 0; i < n; i++) {\n            final int curInd = word1.charAt(i) - \'a\';\n\n            if (freq2[curInd] < freq1[curInd]) count++;\n            if (freq1[curInd] != 0) freq2[curInd]++;\n\n            while(j <= i && count >= m) {\n                ans += (n - i);\n\n                final int prevInd = word1.charAt(j) - \'a\';\n\n                if (freq1[prevInd] != 0) freq2[prevInd]--; \n                if (freq2[prevInd] < freq1[prevInd]) count--;\n\n                j++;\n            }\n        }\n\n        return ans;\n    }\n}\n```
['Java']