Sliding window intuition using Maps
Intuition\n Describe your first thoughts on how to solve this problem. \nFirst get the window having all the characters required, then progressively reduce the 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst get the window having all the characters required, then progressively reduce the window size ensuring the contranst are met.\nAnd substring would be word.length - index\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSliding window to get get all lements required.\nThen get the substrings in the current position by difference of word length and current index.\nBecause if all the required chars are already prsent as prefix, rest of characters can just add to substring by selecting oneor more characters among them.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n) where n is length of word\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(26) ~ O(1) constant space\n\n# Code\n```java []\nclass Solution {\n    public long validSubstringCount(String word1, String word2) {\n        Map<Character, Integer> freq = new HashMap<>();\n        for(Character c : word2.toCharArray()){\n            freq.put(c, freq.getOrDefault(c, 0)+1);\n        }\n        Map<Character, Integer> ongoing = new HashMap<>();\n        int count = 0;\n        int left = 0; \n        int wordCount = 0;\n        long answer = 0;\n        int right=0;\n        while(right < word1.length()){\n            Character curr = word1.charAt(right);\n            if(freq.containsKey(curr)){\n                ongoing.put(curr,ongoing.getOrDefault(curr,0)+1);\n                if(freq.get(curr).equals(ongoing.get(curr)))count++;\n            }\n            while(count == freq.size()){\n                answer += word1.length() - right;\n                Character leftChar = word1.charAt(left);\n                if(freq.containsKey(leftChar)){\n                    ongoing.put(leftChar, ongoing.get(leftChar)-1);\n                    if(ongoing.get(leftChar) < freq.get(leftChar)){\n                        count--;\n                    }\n                }\n                left++;\n            }\n            right++;\n        }\n        return answer;\n    }\n}\n```
['Hash Table', 'Sliding Window', 'Java']