Beat 98% C++ Sliding Window
Intuition\n Describe your first thoughts on how to solve this problem. \nIn the type of constraint problem(give you the definition of string, then count the num
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn the type of constraint problem(give you the definition of string, then count the number of that string), usually need to solve one of the constraints first, then you can solve the problem easily. This problem also follows the rule.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst, we need to split the original string into some strings, each string must fit that \n**the absolute difference value of consecutive character <= 2**\n then we can solve the second constraint.\n\nFor the first constraint, we only need to check there exists some substring in a split string that satisfies its length equal $i*k$ $\\forall i \\in[1, 26]$, and the frequency of each character must equal $k$.\n\nTo find these strings, we can apply the sliding window method in each split string, then we can check all possible strings in $O(n)$ time.\n\n# Complexity\n- Time complexity: $O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $O(1)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    bool check(int cnt[26], int k){\n        for(int i=0;i<26;i++){\n            if(cnt[i] !=0 && cnt[i] !=k)\n                return false;\n        }\n        return true;\n    }\n    int count(int s, int e, string &word, long long k){\n        int ans = 0;\n        for(int i=1;i<=26;i++){\n            if(i*k>e-s)\n                break;\n            long long len = i*k;\n            int cnt[26] = {0};\n            for(int j=s;j<s+len;j++)\n                cnt[word[j]-\'a\']++;\n            if(check(cnt, k))\n                ans++;\n            for(int j=s+len;j<e;j++){\n                cnt[word[j]-\'a\']++;\n                cnt[word[j-len]-\'a\']--;\n                if(check(cnt, k))\n                    ans++;\n            }\n        }\n        return ans;\n    }\n    int countCompleteSubstrings(string word, int k) {\n        int ans = 0;\n        for(int i=0;i<word.size();i++){\n            int start = i;\n            while(i<word.size()-1 && abs(word[i]-word[i+1])<=2)\n                i++;\n            ans += count(start, i+1, word, k);\n        }\n        return ans;\n    }\n};\n```
['Sliding Window', 'C++']