Sliding window + keeping track of the last index where adjacent letters are too distant
Window lengths can only be multiplies of k, and ther can be at most len(set(word)) of letters that all appear k times in the window. \n\nHaving been clear about
Window lengths can only be multiplies of k, and ther can be at most len(set(word)) of letters that all appear k times in the window. \n\nHaving been clear about this, keeping track of the frequency window is an easy task using a loop **for length in range(k,(letters+1)*k,k) ** in each of which window of length i*k is observed.\n\nThe next question would be to satisfy \'adjacent letters cannot be more than 2 positions apart in the alphabet. It was hinted to use sortedlist or heap, but that turned out to be TLE. Rather, one loop through the word would be enough to record all indexs where two adjacent letters are too distant, and all we need to do in the moving window loop is to see whether there is an infeasible index within in the window, which can be done quite easily by using a helper index denoting the rightmost infeasible index seen so far.\n\n# Code\n```\nfrom sortedcontainers import SortedList\nclass Solution:\n    def countCompleteSubstrings(self, word: str, k: int) -> int:\n        letters = len(set(word))\n        n = len(word)\n        res = 0\n        diffs = []\n        for i in range(n):\n            if i > 0 and abs(ord(word[i]) - ord(word[i-1])) > 2:\n                diffs.append(i-1)\n        \n        m = len(diffs)\n        for length in range(k,(letters+1)*k,k):\n            freq = {}\n            i = 0\n            currdiff = -1\n            while i < n:\n                a = word[i]\n                if a not in freq:\n                    freq[a] = 0\n                freq[a] += 1\n                if i > length - 1:\n                    freq[word[i-length]] -= 1\n                    if freq[word[i-length]] == 0:\n                        del freq[word[i-length]]\n                if i >= length - 1 and (currdiff < 0 or diffs[currdiff] <= i - length) and all(freq[l] == k for l in freq):\n                    res += 1\n                i += 1\n                if currdiff < m - 1 and i > diffs[currdiff+1]:\n                    currdiff += 1\n\n        return res \n                \n\n\n\n        \n```
['Python3']