Standard Sliding Window | C++ | JAVA | Clean Variables | codestorywithMIK
My YouTube Video on this Problem - https://www.youtube.com/watch?v=ygPMLjxaT2Y\n\n/****************************************************************** C++ ******
My YouTube Video on this Problem - https://www.youtube.com/watch?v=ygPMLjxaT2Y\n```\n/****************************************************************** C++ ********************************************/\n//Approach (Using sliding window)\n//T.C : O(n*26*n) ~ O(n^2)\n//S.C : O(26) We create an array of size 26 everytime. So assuming it constant\nclass Solution {\npublic:\n    int solve(int start, int end, string &word, int k) {\n        int result = 0;\n        for(int chars = 1; chars <= 26 && chars*k <= end-start+1; chars++) {\n\n            vector<int> count(26, 0);\n            int goodChars = 0; //How many chars are of frequency k\n\n            //Now do a sliding window\n            int i = start;\n            int windowLength = chars*k;\n            for(int j = start; j <= end; j++) {\n                char ch = word[j];\n\n                count[ch - \'a\']++;\n                if(count[ch - \'a\'] == k) {\n                    goodChars++;\n                } else if(count[ch - \'a\'] == k+1) {\n                    goodChars--;\n                }\n\n                if(j-i+1 > windowLength) { //Need to shift window right\n                    if(count[word[i] - \'a\'] == k) {\n                        goodChars--;\n                    } else if(count[word[i] - \'a\'] == k+1) {\n                        goodChars++;\n                    }\n                    count[word[i] - \'a\']--;\n                    i++;\n                }\n\n                if(goodChars == chars) {\n                    result++;\n                }\n\n            }\n\n        }\n\n        return result;\n\n    }\n\n    int countCompleteSubstrings(string word, int k) {\n        int n = word.length();\n\n        int result = 0;\n        int last = 0;\n\n        for(int i = 1; i <= n; i++) {\n            if(i == n || abs(word[i] - word[i-1]) > 2) { //satisying condition 2\n                result += solve(last, i-1, word, k); //condition 1\n                last = i;\n            }\n        }\n\n        return result;\n    }\n};\n\n\n/****************************************************************** JAVA *******************************************/\n//Approach (Using sliding window)\n//T.C : O(n*26*n) ~ O(n^2)\n//S.C : O(26) We create an array of size 26 everytime. So assuming it constant\nclass Solution {\n    public int solve(int start, int end, String word, int k) {\n        int result = 0;\n        for (int chars = 1; chars <= 26 && chars * k <= end - start + 1; chars++) {\n            int[] count = new int[26];\n            int goodChars = 0; // How many chars are of frequency k\n\n            // Now do a sliding window\n            int i = start;\n            int windowLength = chars * k;\n            for (int j = start; j <= end; j++) {\n                char ch = word.charAt(j);\n\n                count[ch - \'a\']++;\n                if (count[ch - \'a\'] == k) {\n                    goodChars++;\n                } else if (count[ch - \'a\'] == k + 1) {\n                    goodChars--;\n                }\n\n                if (j - i + 1 > windowLength) { // Need to shift window right\n                    if (count[word.charAt(i) - \'a\'] == k) {\n                        goodChars--;\n                    } else if (count[word.charAt(i) - \'a\'] == k + 1) {\n                        goodChars++;\n                    }\n                    count[word.charAt(i) - \'a\']--;\n                    i++;\n                }\n\n                if (goodChars == chars) {\n                    result++;\n                }\n            }\n        }\n        return result;\n    }\n\n    public int countCompleteSubstrings(String word, int k) {\n        int n = word.length();\n        int result = 0;\n        int last = 0;\n\n        for (int i = 1; i <= n; i++) {\n            if (i == n || Math.abs(word.charAt(i) - word.charAt(i - 1)) > 2) { // satisfying condition 2\n                result += solve(last, i - 1, word, k); // condition 1\n                last = i;\n            }\n        }\n        return result;\n    }\n}\n```
['C', 'Java']