Multiset Causing TLE → Divide the String First
First try, i just translated hints to c++ codeThis caused TLE like this:

That means this just won't work. I balme that for the multiset part.Try 2, i went to s
# First try, i just translated hints to c++ code
This caused TLE like this:
<img src = https://assets.leetcode.com/users/images/0c622b8d-4a26-424e-b671-ba324146bf61_1734705875.0463495.png width = 70%>
That means this just won't work. I balme that for the multiset part. 


```C++

class Solution {
public:
    int countCompleteSubstrings(string word, int k) {
        int n = word.size(), ans = 0;
        for(int i = 0; i < 26; i ++)
            if(word == string(n, 'a' + i))
                return n - k + 1;
        for(int len = 1; (len <= 26) && (k * len <= n); len ++)
        {
            multiset<int>st; 
            vector<int> cnt(26, 0); int diffchars = 0;
            for(int i = 0; i < k * len - 1; i ++)
            {
                if(++ cnt[word[i] - 'a'] == k) diffchars ++;
                if(cnt[word[i] - 'a'] == k + 1) diffchars --;
                if(i)st.insert(abs(word[i] - word[i - 1]));
            }
            for(int i = k * len - 1; i < n; i ++)
            {
                // process the 'right'
                if(++ cnt[word[i] - 'a'] == k) diffchars ++;
                if(cnt[word[i] - 'a'] == k + 1) diffchars --;
                if(i && (len * k > 1))st.insert(abs(word[i] - word[i - 1]));
                // adjust 'ans'
                if((diffchars == len) && (st.empty() || (*rbegin(st) <= 2)))
                {
                    // cout << len << " " << i << endl;
                    ans ++;
                }
                  
                if(i == n - 1)break;
                // pop 'left'
                if(-- cnt[word[i - (k * len - 1)] - 'a'] == k) diffchars ++;
                if(cnt[word[i - (k * len - 1)] - 'a'] == k - 1) diffchars --;
                if(k * len > 1)st.erase(st.find(abs(word[i - (k * len - 1) + 1] - word[i - (k * len - 1)])));
            }
        }
        return ans;
    }
};
auto init = []()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    return nullptr;
}();
```

## Try 2, i went to see other's sol and adjust my code
[This sol is written in chinese](https://leetcode.cn/problems/count-complete-substrings/solutions/2551743/bao-li-hua-chuang-mei-ju-chuang-kou-nei-31j5m/)

```C++
class Solution {
public:
    int countCompleteSubstrings(string word, int k) {
        int n = word.size(), ans = 0;
        // Unnecessary now, can be added to cause less Runtime but more Memeory cost 
        // for(int i = 0; i < 26; i ++)
        //     if(word == string(n, 'a' + i))
        //         return n - k + 1;
        auto count = [&](string_view s)
        {
            for(int len = 1; (len <= 26) && (k * len <= s.size()); len ++)
            {
                vector<int> cnt(26, 0); int diffchars = 0;
                for(int i = 0; i < k * len - 1; i ++)
                {
                    if(++ cnt[s[i] - 'a'] == k) diffchars ++;
                    if(cnt[s[i] - 'a'] == k + 1) diffchars --;
                }
                for(int i = k * len - 1; i < n; i ++)
                {
                    // process the 'right'
                    if(++ cnt[s[i] - 'a'] == k) diffchars ++;
                    if(cnt[s[i] - 'a'] == k + 1) diffchars --;
                    // adjust 'ans'
                    if(diffchars == len) 
                    {
                        // cout << len << " " << i << endl;
                        ans ++;
                    }
                    
                    if(i == s.size() - 1)break;
                    // pop 'left'
                    if(-- cnt[s[i - (k * len - 1)] - 'a'] == k) diffchars ++;
                    if(cnt[s[i - (k * len - 1)] - 'a'] == k - 1) diffchars --;
                }
            }
        };
        string_view ss(word);
        for (int i = 0; i < n;) {
            int st = i;
            for (i++; i < n && abs(int(word[i]) - int(word[i - 1])) <= 2; i++);
            count(ss.substr(st, i - st));
        }
        return ans;
    }
};
auto init = []()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    return nullptr;
}();
```
![image.png](https://assets.leetcode.com/users/images/394229a5-310a-42af-af7f-5092a17dfa57_1734706239.8594122.png)

['String', 'Sliding Window', 'C++']