Sliding Window | Beats 100% | Frequency Array
Intuition\nWe can see from the problem that if we find a string that satisfies the condition, we can keep adding each character left in the whole string and it 
# Intuition\nWe can see from the problem that if we find a string that satisfies the condition, we can keep adding each character left in the whole string and it would still satisfy this condition. We know that we should then find every first occurence that satisfies the condition, and then add the amount of characters after its end state.\n\n# Approach\nWe initialize a freq array to keep track of the frequency of characters in the string. When we uncounter a substring that satisfies the condition, we add the amount of characters after the right pointer. Because we then know we have account for every possible string that starts at the left pointer, we increment it by one.\n\nWe make sure to track the prequencies upon the movement of the pointers.\n\nNote - I check if the right is within the range whenever I check, because it will always attempt to reach out of the array.\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```java []\nclass Solution {\n    public int numberOfSubstrings(String s, int k) {\n        int[] freq = new int[26];\n\n        int left = 0;\n        int right = 0;\n\n        char[] chars = s.toCharArray();\n\n        freq[chars[0] - \'a\'] = 1;\n\n        int sum = 0;\n\n        while(right != s.length()) {\n            if(freq[chars[right] - \'a\'] < k) {\n                right++;\n                if(right < s.length()) {\n                    freq[chars[right] - \'a\']++;\n                }\n            } else {\n                freq[chars[left] - \'a\']--;\n                sum += s.length() - right;\n                left++;\n            }\n        }\n\n        return sum;\n    }\n}\n```
['Java']