Similar to Leetcode#395, Sliding Window, beat 100%, O(26N)
When I first saw this question, it immediately reminds me of 395. Longest Substring with At Least K Repeating Characters\nAs a result, I extend the solution of 
When I first saw this question, it immediately reminds me of [395. Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/)\nAs a result, I extend the solution of 395 to fulfill this question\'s requirement.\n\nBasically, it uses a sliding window technique.\nThe only difference between #395 and this question is the criteria of the window is much more strict\n1. the difference between adjacent characters **cannot** exceed 2\n2. each unique character **must** appear k times\n\nAs we recongnize the difference, the last thing we need to do is adding the above constraint to the #395\'s solution.\n```\nclass Solution {\npublic:\n    int count_unique(string &s, int k, int unique_target) {\n        int m[26] = {0};\n        int n = s.size();\n        int nolessk = 0; /* record number of character that has appeared k times */\n        int unique = 0; /* record how many unique character appeared */\n        int right = 0;\n        int left = 0;\n        int res = 0;\n        \n        while (right < n) {\n            int c = s[right] - \'a\';\n            \n            if (m[c] == 0) /* a new unique character occurred */\n                unique++;\n            if (++m[c] == k) /* a character appeared k times */\n                nolessk++;\n            \n\t\t\t/* 1st criteria broken, shrink the window to fulfill the criteria */\n            while (right > 0 && left < right && abs(s[right] - s[right - 1]) > 2) { \n                int d = s[left++] - \'a\';\n                \n                if (m[d]-- == k)\n                    nolessk--;\n                if (m[d] == 0)\n                    unique--;\n            }\n            \n\t\t\t/* \n\t\t\t * 2nd criteria broken, shrink the window to fulfill the criteria\n\t\t\t * either the current window has more unique character than we need or a character appeared more than k times\n\t\t\t */\n            while (left < right && (unique > unique_target || m[c] > k)) {\n                int d = s[left++] - \'a\';\n                \n                if (m[d]-- == k)\n                    nolessk--;\n                if (m[d] == 0)\n                    unique--;\n            }\n            \n            if (unique == unique_target && unique == nolessk) {\n                res++;\n            }\n            right++;\n        }\n        \n        return res;\n    }\n    \n    int countCompleteSubstrings(string word, int k) {\n        int res = 0;\n        \n        for (int i = 1; i <= 26; i++)\n            res += count_unique(word, k, i);\n        \n        return res;\n    }\n};\n```\n\nThe solution may not be very concise (since I use 2 while loops to shrink the window), but I think it is pretty clear as long as we are familiar with #395.\n\nMy #395\'s solution is based on this post\nhttps://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/87739/Java-Strict-O(N)-Two-Pointer-Solution\n\nP.S. The original solution, I used unordered_map to record each character\'s occurrence but it TLE. Then, I changed it to simple array and it passed.
['Sliding Window']