Clean C++ Solution of O(N) complexity | Beats 100% in runtime
Intuition\n Describe your first thoughts on how to solve this problem. \n1. If adjacent charachers are more than 2 distance apart, we can break the string there
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. If adjacent charachers are more than 2 distance apart, we can break the string there ans solve the given problem for the smaller string.\n2. We have 26 characters in the alphabet, so possible lengths of the substring are 1*k, 2*k, 3*k,....,26*k because each character occurs exactly k times.\n3. We use sliding window approach for each length and maintain a frequency map of characters. For each window we check the frequency map, if each character occurs exactly k times then it\'s a valid substring.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nWe are iterating the length of the array for 26 times. and each time we are iterating over the frequency map of length 26.\nSo the time complexity is O(26x26xN)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nSpace complexity is O(1) as we are using a map of size 26 which is fixed.\n\n# Code\n```\nclass Solution {\npublic:\n    bool check(unordered_map<char,int>&count, int k){\n        for (auto x: count){\n            if (x.second==0) continue;\n            if (x.second!=k) return false;\n        }\n        return true;\n    }\n    int util(string &s, int l, int r, int k){\n        int ans=0;\n        for (int x=1;x<=26;x++){\n            int len=x*k;\n            if (len>(r-l+1)) break;\n            //sliding window\n            unordered_map<char,int>count;\n            for (int i=0;i<len;i++){\n                count[s[l+i]]++;\n            }\n            if (check(count,k)) ans++;\n            for (int i=l+len;i<=r;i++){\n                count[s[i-len]]--;\n                count[s[i]]++;\n                if (check(count,k)){\n                    ans++;\n                }\n            }\n\n        }\n        return ans;\n    }\n    int countCompleteSubstrings(string s, int k) {\n        int st=0;\n        int ans=0;\n        for (int i=1;i<s.length();i++){\n            if (abs(s[i]-s[i-1])>2){\n                ans+=util(s,st,i-1,k);\n                st=i;\n            }\n        }\n        ans+=util(s,st,s.length()-1,k);\n        return ans;\n    }\n};\n```
['C++']