Sliding Window | Well Explained
\n\n# Code\n\nclass Solution {\npublic:\n    int freq[128] = {0}, nuc, nck;\n\n    // Function to count complete substrings with given properties\n    int func(
![image.png](https://assets.leetcode.com/users/images/01782ec6-f3fa-44a0-b0ec-5f13d1de80d8_1710159835.512464.png)\n\n# Code\n```\nclass Solution {\npublic:\n    int freq[128] = {0}, nuc, nck;\n\n    // Function to count complete substrings with given properties\n    int func(string& s, int lo, int hi, int& k) {\n        int ans = 0;\n\n        // Iterate over different substring lengths\n        for (int i = 1; i < 27; i++) {\n            int len = i * k;\n\n            // Break if the current substring length exceeds the available range\n            if (len > (hi - lo)) break;\n\n            nuc = 0, nck = 0;\n\n            // Initialize frequencies for the current substring\n            for (int j = lo; j < (lo + len); j++) {\n                freq[s[j]]++;\n                if (freq[s[j]] == 1) nuc++;\n                if (freq[s[j]] == k) nck++;\n                else if (freq[s[j]] == k + 1) nck--;\n            }\n\n            int itr = lo, ita = lo + len;\n\n            // Check properties of each subsequent substring\n            while (ita < hi) {\n                ans += (nuc == nck);\n\n                if (freq[s[itr]] == 1) nuc--;\n                if (freq[s[itr]] == k) nck--;\n                else if (freq[s[itr]] == k + 1) nck++;\n                freq[s[itr]]--;\n\n                freq[s[ita]]++;\n                if (freq[s[ita]] == 1) nuc++;\n                if (freq[s[ita]] == k) nck++;\n                else if (freq[s[ita]] == k + 1) nck--;\n\n                itr++, ita++;\n            }\n\n            // Check properties of the last substring\n            ans += (nuc == nck);\n\n            // Reset frequencies for the next iteration\n            while (itr < hi) --freq[s[itr++]];\n        }\n\n        return ans;\n    }\n\n    // Function to count complete substrings in the given word\n    int countCompleteSubstrings(string& word, int k) {\n        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n        int ans = 0, n = word.length(), curr = 0, prev = 0;\n\n        // Iterate over the word to identify substrings with specific properties\n        for (; curr < n - 1; curr++)\n            if (abs(word[curr] - word[curr + 1]) > 2)\n                ans += func(word, prev, curr + 1, k), prev = curr + 1;\n\n        // Include the last substring\n        ans += func(word, prev, curr + 1, k);\n\n        return ans;\n    }\n};\n```
['Hash Table', 'String', 'Sliding Window', 'C++']