3297. Count Substrings That Can Be Rearranged to Contain a String I. Solutions
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        int n = word1.length();\n        int m = word2.length();\n        if (m > n) return 0;\n\n        HashMap<Character, Integer> count2 = new HashMap<>();\n        HashMap<Character, Integer> count1 = new HashMap<>();\n\n        for (char c : word2.toCharArray()) {\n            count2.put(c, count2.getOrDefault(c, 0) + 1);\n        }\n\n        long result = 0;\n        int required = count2.size();\n        int formed = 0;\n        int left = 0;\n\n        for (int right = 0; right < n; ++right) {\n            char c = word1.charAt(right);\n            count1.put(c, count1.getOrDefault(c, 0) + 1);\n\n            if (count2.containsKey(c) && count1.get(c).intValue() == count2.get(c).intValue()) {\n                formed++;\n            }\n\n            while (formed == required) {\n                result += (n - right);\n                char leftChar = word1.charAt(left);\n                count1.put(leftChar, count1.get(leftChar) - 1);\n                \n                if (count2.containsKey(leftChar) && count1.get(leftChar) < count2.get(leftChar)) {\n                    formed--;\n                }\n                left++;\n            }\n        }\n\n        return result;\n    }\n}\n```
['Java']