C++ preprocessing with prefix and tracking indexing, then iteration
I took a look at Votrubac\'s solution and I like the idea much more (and the code is much shorter and more efficient).\n\ncpp []\nclass Solution {\npublic:\n   
I took a look at Votrubac\'s solution and I like the idea much more (and the code is much shorter and more efficient).\n\n```cpp []\nclass Solution {\npublic:\n    bool valid(array<int, 26>& v, int k){\n        bool a = false;\n        for (auto& n : v)\n            if (n && n != k) return false;\n            else a = a || (n == k);\n        return a;\n    }\n    int countCompleteSubstrings(string w, int k) {\n        vector<int> twoLeftBound = {0};\n        int ans = 0;\n        for (int i = 1, l = 0; i < w.size(); ++i){\n            char a = min(w[i], w[i - 1]),\n            b = max(w[i], w[i - 1]);\n            int dist = b - a;\n            if (dist > 2) l = i;\n            twoLeftBound.push_back(l);\n        }\n        vector<array<int, 26>> p;\n        p.push_back({});\n        array<vector<int>, 26> ti = {};\n        array<int, 26> cur = {};\n        for (int i = 0; i < w.size(); ++i){\n            int ind = w[i] - \'a\';\n            ++cur[ind];\n            ti[ind].push_back(i);\n            p.push_back(cur);\n        }\n        for (int r = 0; r < w.size(); ++r){\n            int l = 0;\n            array<int, 26> charFreq = p[r + 1];\n            for (int i = 0; i < charFreq.size(); ++i){\n                int dif = charFreq[i] - k;\n                if (dif > 0) l = max(l, ti[i][dif - 1] + 1);\n            }\n            while (1){\n                if (l >= twoLeftBound[r]){\n                    array<int, 26> charDif = charFreq;\n                    for (int i = 0; i < charDif.size(); ++i)\n                        charDif[i] -= p[l][i];\n                    ans += valid(charDif, k);\n                }\n                if (l <= r){\n                    int posC = w[l] - \'a\';\n                    int amountToZero = charFreq[posC];\n                    l = ti[posC][amountToZero - 1] + 1;\n                    if (l > r) break;\n                }\n                else break;\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']