[C++] Sliding Window and Count Frequency
Intuition\n Describe your first thoughts on how to solve this problem. \n- Slide a window and track how many character occurs exactly k times\n- Condition 1\n  
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Slide a window and track how many character occurs exactly `k` times\n- Condition 1\n  - The window size must be `1 * k`, `2 * k`, ..., `26 * k`\n    - Because each character must occur exactly `k` times\n    - There maybe\n      - `1 * k`: `1` character occurs `k` times\n      - `2 * k`: `2` character occurs `k` times\n      - `...`\n      - `26 * k`: `26` character occurs `k` times\n- Condition 2\n  - If we violate this rule, then\n    - Reset the frequency table\n    - Reset the window size to `1`\n    - Reset the counter of character with frequency `k`\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Try all possible window sizes (`1 * k`, `2 * k`, ..., `26 * k`)\n- Check conditions and slide the window\n\n# Complexity\n- Time complexity: $$O({26}\\times{n}) = O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(26) = O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    // time/space: O(n)/O(1)\n    int countCompleteSubstrings(string word, int k) {\n        int n = word.size();\n        int result = 0;\n        for (int x = 1; x <= 26; x++) {\n            // condition 1\n            int length = x * k;     \n            if (length > n) break;\n            \n            vector<int> freq(26, 0);\n            int countFreqK = 0;\n            for (int l = 0, r = 0; r < n; r++) {\n                // check the condition 2\n                if ((r > 0) && (abs(word[r - 1] - word[r]) > 2)) {\n                    fill(freq.begin(), freq.end(), 0);\n                    countFreqK = 0;\n                    l = r;\n                }\n                // update the frequency\n                if (freq[word[r] - \'a\'] == k) countFreqK--;         // freq `k` to `k + 1`\n                freq[word[r] - \'a\']++;\n                if (freq[word[r] - \'a\'] == k) countFreqK++;         // freq `k - 1` to `k`\n                // limit the windows size with `x * k`\n                if ((r - l + 1) > length) {\n                    // remove the left character and update the frequency\n                    if (freq[word[l] - \'a\'] == k) countFreqK--;     // freq `k` to `k - 1`\n                    freq[word[l] - \'a\']--;\n                    if (freq[word[l] - \'a\'] == k) countFreqK++;     // freq `k + 1` to `k`\n                    l++;\n                }\n                // update the result\n                if (((r - l + 1) == length) && (countFreqK == x)) result++;\n            }\n        }\n        return result;\n    }\n};\n```
['Sliding Window', 'Counting', 'C++']