sliding window different way of working with condition 2 adjacent character distance
Intuition\n Describe your first thoughts on how to solve this problem. \nyada yada same as most people each substring has to be some multiple of k where that mu
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nyada yada same as most people each substring has to be some multiple of k where that multiple would correspond to the number of unique characters in the substring and since we only have lowercase letters i.e. upto 26 unique characters are substrings are of length 1*k, 2*k,.., 26*k.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nusing sliding window with window sizes of 1*k, 2*k, and so on keep track of the frequency of characters in the window. now the interesting part handling the adjacenty condition an easy way to do this is to keep track of the right most bad character i.e. the adjacent characters that are too far apart. we can update this value everytime we move are window, and if are window contains the bad index we don\'t check it.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nlinear\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nconstant\n# Code\n```\nclass Solution {\npublic:\n    bool valid(vector<int>& freq, int k) { \n        for (int x = 0; x < 26; ++x) {\n            if (freq[x] != 0 && freq[x] != k) { \n                return false;\n            }\n        }\n        return true;\n    }\n\n    int countCompleteSubstrings(string word, int k) {\n        int ans = 0;\n        for (int i = 1; i <= 26; ++i) {\n            if (i * k > word.size()) { continue; }\n\n            vector<int> freq(26);\n            int bad_index = -1;\n            for (int j = 0; j < i * k; ++j) { \n                freq[word[j] - \'a\']++;\n                if (j > 0 && abs(word[j] - word[j - 1]) > 2) { \n                    bad_index = j - 1;\n                }\n            }\n            if (bad_index == -1 && valid(freq, k)) { ++ans; }\n\n            for (int j = i * k; j < word.size(); ++j) { \n                freq[word[j - i * k] - \'a\']--;\n                freq[word[j] - \'a\']++;\n                if (abs(word[j] - word[j - 1]) > 2) { \n                    bad_index = j - 1;\n                }\n                if (j - i * k + 1 <= bad_index && bad_index <= j) { continue; }\n                if (valid(freq, k)) { ++ans; }\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']