Easiest Solution 🔥✅ | Sliding Window | BEATS 94.94%
Intuition\nWe need to count substrings where at least one character appears exactly k times. A sliding window approach helps us track character frequencies and 
# Intuition\nWe need to count substrings where at least one character appears exactly `k` times. A sliding window approach helps us track character frequencies and adjust the window to find valid substrings efficiently.\n\n# Approach\n1. Use a sliding window with a pointer `l` and a frequency map `d` to track characters in the window.\n2. For each character, update its frequency in `d`.\n3. If any character\'s frequency reaches `k`, shrink the window by incrementing `l`.\n4. Add the number of valid starting positions (`l`) to the result for each window.\n5. Return the total count of valid substrings.\n\n# Complexity\n- Time complexity: $$O(n)$$, where n is the length of the string.\n\n- Space complexity: $$O(1)$$, since the maximum number of items we store in our hashmap is 26 (every letter).\n\n# Code\n```python3 []\nclass Solution:\n    def numberOfSubstrings(self, s: str, k: int) -> int:\n        ans = 0\n        l = 0\n        d = {}\n        for c in s:\n            d[c] = d.get(c, 0) + 1\n            while d[c] == k:\n                d[s[l]] -= 1\n                l += 1\n            ans += l\n        return ans\n```\n```cpp []\nclass Solution {\npublic:\n    int numberOfSubstrings(string s, int k) {\n        int ans = 0;\n        int l = 0;\n        unordered_map<char, int> d;\n        \n        for (char c : s) {\n            d[c]++;\n            while (d[c] == k) {\n                d[s[l]]--;\n                l++;\n            }\n            ans += l;\n        }\n        \n        return ans;\n    }\n};\n```\n```java []\nimport java.util.HashMap;\n\nclass Solution {\n    public int numberOfSubstrings(String s, int k) {\n        int ans = 0;\n        int l = 0;\n        HashMap<Character, Integer> d = new HashMap<>();\n        \n        for (char c : s.toCharArray()) {\n            d.put(c, d.getOrDefault(c, 0) + 1);\n            \n            while (d.get(c) == k) {\n                d.put(s.charAt(l), d.get(s.charAt(l)) - 1);\n                l++;\n            }\n            \n            ans += l;\n        }\n\n        return ans;\n    }\n}\n```\n![image.png](https://assets.leetcode.com/users/images/0e072e9b-4678-4507-8b78-beefacf5cba8_1729700815.6733763.png)\n
['Sliding Window', 'C++', 'Java', 'Python3']