[k, 2k, ...26k] Sliding Windows
We can have 1, 2, ..., 26 unique characters in the string.\n\nThat number of characters defines the size of our sliding window.\n\nAs we slide, we track how man
We can have 1, 2, ..., 26 unique characters in the string.\n\nThat number of characters defines the size of our sliding window.\n\nAs we slide, we track how many characters appear exactly `k` times.\n\nNote that when the difference between two adjacent characters exceeds `2`, we restart the window.\n\n**C++**\n```cpp\nint countCompleteSubstrings(const string &w, int k) {\n    int res = 0;\n    for (int chars = 1; chars <= min((int)w.size() / k, 26); ++chars) {\n        int cnt[26] = {}, exact = 0, sz = chars * k;\n        for (int i = 0, j = 0; i < w.size(); ++i) {\n            if (i && (abs(w[i] - w[i - 1]) > 2)) {\n                memset(cnt, 0, sizeof(cnt));\n                exact = 0;\n                j = i;\n            }\n            int reps = ++cnt[w[i] - \'a\'];\n            exact += reps == k ? 1 : reps == k + 1 ? -1 : 0;\n            if (i - j + 1 >= sz) {\n                if (i - j >= sz) {\n                    int reps = --cnt[w[j++] - \'a\'];\n                    exact += reps == k ? 1 : reps == k - 1 ? -1 : 0;\n                }\n                res += exact == chars;\n            }\n        }\n    }\n    return res;\n}\n```\n**Python**\nThe equivalent Python code needs a bit of optimization to pass the time limits.\n```python\nclass Solution:\n    def countCompleteSubstrings(self, w: str, k: int) -> int:\n        res = 0\n        for chars in range(1, min(len(w) // k, 26) + 1):\n            exact, sz, cnt = 0, chars * k, [0] * 26\n            dirty = False\n            j = 0\n            for i in range(0, len(w)):\n                if i and abs(ord(w[i]) - ord(w[i - 1])) > 2:\n                    exact, dirty = 0, True\n                    j = i\n                if dirty:\n                    dirty = False\n                    cnt = [0] * 26\n                cnt[ord(w[i])- ord(\'a\')] += 1\n                reps = cnt[ord(w[i]) - ord(\'a\')]\n                exact += 1 if reps == k else -1 if reps == k + 1 else 0\n                if i - j + 1 >= sz:\n                    if i - j >= sz:\n                        cnt[ord(w[j]) - ord(\'a\')] -= 1\n                        reps = cnt[ord(w[j]) - ord(\'a\')]\n                        exact += 1 if reps == k else -1 if reps == k - 1 else 0\n                        j += 1\n                    res += exact == chars\n        return res\n```
['C']