<p>You are given a string <code>word</code> and an integer <code>k</code>.</p>

<p>A substring <code>s</code> of <code>word</code> is <strong>complete</strong> if:</p>

<ul>
	<li>Each character in <code>s</code> occurs <strong>exactly</strong> <code>k</code> times.</li>
	<li>The difference between two adjacent characters is <strong>at most</strong> <code>2</code>. That is, for any two adjacent characters <code>c1</code> and <code>c2</code> in <code>s</code>, the absolute difference in their positions in the alphabet is <strong>at most</strong> <code>2</code>.</li>
</ul>

<p>Return <em>the number of <strong>complete </strong>substrings of</em> <code>word</code>.</p>

<p>A <strong>substring</strong> is a <strong>non-empty</strong> contiguous sequence of characters in a string.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> word = &quot;igigee&quot;, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: <u><strong>igig</strong></u>ee, igig<u><strong>ee</strong></u>, <u><strong>igigee</strong></u>.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> word = &quot;aaabbbccc&quot;, k = 3
<strong>Output:</strong> 6
<strong>Explanation:</strong> The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are: <strong><u>aaa</u></strong>bbbccc, aaa<u><strong>bbb</strong></u>ccc, aaabbb<u><strong>ccc</strong></u>, <strong><u>aaabbb</u></strong>ccc, aaa<u><strong>bbbccc</strong></u>, <u><strong>aaabbbccc</strong></u>.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li>
	<li><code>word</code> consists only of lowercase English letters.</li>
	<li><code>1 &lt;= k &lt;= word.length</code></li>
</ul>

class Solution:
    def countCompleteSubstrings(self, word: str, k: int) -> int:
        # s 中有几个子串 -- 使每个字母恰好出现k次
        # 枚举子串有多少种字母 m
        # 长度恰好为 m * k的窗口内，是否满足字母恰好初心k次
        def function_count(s:str):
            res = 0
            for m in range(1, 27):
                size = m * k #窗口多大
                if size > len(s):
                    break #窗口已经比字符串大了就没必要了
                cnt = Counter(s[:size]) # -- 长为26数组也ok
                if all(c == k for c in cnt.values()):
                    res += 1
                for in_, out in zip(s[size:], s):
                    cnt[in_] += 1
                    cnt[out] -= 1
                    if all(c == 0 or c == k for c in cnt.values()):
            #Count里面可能为0，所以你滑动的时候要不就是0要不就是k，别的就不可以哦！！！
                        res += 1
            return res
        # 你想一下是不是可以将一个大的string分为很多几个小的满足 two adjacent 的差 <= 2
        # aaaad -- 例如这个 你要知道从哪开始到哪结束 [start:i] - 这个区间就是满足要求的小的string
        # 所以要记录start， 到aaaa | d 切开， 最后一次i指向d
        n = len(word)
        ans = i = 0
        while i < n:
            start = i
            i += 1
            while i < n and abs(ord(word[i]) - ord(word[i-1])) <= 2:
                i += 1
            ans += function_count(word[start:i])
        return ans
3854