🥇 C++ | PYTHON | JAVA  ; ] ✅
\n- Segment the string into parts ensuring that no two adjacent characters have a difference of more than 2. For each segmented part, Calculate the solution and
\n- Segment the string into parts ensuring that no two adjacent characters have a difference of more than 2. For each segmented part, Calculate the solution and aggregate them into the final result.\n\n- The process of calculating the solution for each part involves counting substrings where each character appears exactly `k` times. Since the string is composed of only 26 unique characters, the length of the substring should be k times the number of unique characters `j`.\n\n- Iterate through all possible substring lengths from `1 * k` to `26 * k`. For a substring of length `j * k`, I use a frequency array to verify that there are `j` characters with a frequency of `k`. The sliding window technique is employed to examine substrings of each length, starting from `1 * k`.\n\n``` C++ []\nimport java.util.HashMap;\nimport java.util.Map;\n\nclass Solution {\n    public int countCompleteSubstrings(String word, int k) {\n        // Define the calc function as a nested function\n        // This is possible only in Java 11 and later\n        var calc = (String s) -> {\n            int res = 0;\n            int v = s.length();\n            for (int i = 1; i <= 26; ++i) {\n                if (i * k > v) break;\n                int l = i * k;\n\n                Map<Character, Integer> cnt = new HashMap<>();\n                for (int j = 0; j < l; ++j) {\n                    cnt.put(s.charAt(j), cnt.getOrDefault(s.charAt(j), 0) + 1);\n                }\n\n                Map<Integer, Integer> freq = new HashMap<>();\n                for (int count : cnt.values()) {\n                    freq.put(count, freq.getOrDefault(count, 0) + 1);\n                }\n\n                if (freq.getOrDefault(k, 0) == i) res++;\n\n                for (int idx = 0; idx < v - l; ++idx) {\n                    int oldCount = cnt.get(s.charAt(idx));\n                    freq.put(oldCount, freq.get(oldCount) - 1);\n                    cnt.put(s.charAt(idx), oldCount - 1);\n                    freq.put(cnt.get(s.charAt(idx)), freq.getOrDefault(cnt.get(s.charAt(idx)), 0) + 1);\n\n                    int newCount = cnt.get(s.charAt(idx + l));\n                    freq.put(newCount, freq.get(newCount) - 1);\n                    cnt.put(s.charAt(idx + l), newCount + 1);\n                    freq.put(cnt.get(s.charAt(idx + l)), freq.getOrDefault(cnt.get(s.charAt(idx + l)), 0) + 1);\n\n                    if (freq.getOrDefault(k, 0) == i) res++;\n                }\n            }\n            return res;\n        };\n\n        int idx = 0;\n        int ans = 0;\n        int n = word.length();\n        for (int i = 1; i < n; ++i) {\n            if (Math.abs(word.charAt(i) - word.charAt(i - 1)) > 2) {\n                ans += calc.apply(word.substring(idx, i));\n                idx = i;\n            }\n        }\n        ans += calc.apply(word.substring(idx));\n        return ans;\n    }\n}\n\n```\n\n``` Python []\nclass Solution:\n    def countCompleteSubstrings(self, w: str, k: int) -> int:\n        def calc(s):\n            res = 0\n            v = len(s)\n            for i in range(1, 27):\n                if i * k > v: break\n                l = i * k\n                cnt = Counter(s[:l])\n                freq = Counter(cnt.values())\n                \n                if freq[k] == i: res += 1\n                \n                for idx in range(v - l):\n                    freq[cnt[s[idx]]] -= 1\n                    cnt[s[idx]] -= 1\n                    freq[cnt[s[idx]]] += 1\n\n                    freq[cnt[s[idx+l]]] -= 1\n                    cnt[s[idx+l]] += 1\n                    freq[cnt[s[idx+l]]] += 1\n\n                    if freq[k] == i: res += 1\n            return res\n        \n        idx = 0\n        ans = 0\n        n = len(w)\n        for i in range(1, n):\n            if abs(ord(w[i]) - ord(w[i-1])) > 2:\n                ans += calc(w[idx:i])\n                idx = i\n        ans += calc(w[idx:])\n        return ans\n```\n\n``` JAVA []\nclass Solution {\n\n\tpublic int countCompleteSubstrings(String word, int k) {\n\t\tint count = 0;\n\t\tfor (int i = 1; i <= 26; i++) {\n\t\t\tcount += countCompleteSubstrings(word, k, 0, i * k);\n\t\t}\n\t\treturn count;\n\t}\n\n\tprivate int countCompleteSubstrings(String word, int k, int start, int length) {\n\t\tif (word.length() - start < length) {\n\t\t\treturn 0;\n\t\t}\n\t\tint map[] = new int[26], count = 0, sum = 0;\n\t\tfor (int i = start; i < word.length(); i++) {\n\t\t\tif (i > start && Math.abs(word.charAt(i) - word.charAt(i - 1)) > 2) {\n\t\t\t\treturn sum + countCompleteSubstrings(word, k, i, length);\n\t\t\t}\n\t\t\tcount += map[word.charAt(i) - \'a\'] == k - 1 ? 1 : 0;\n\t\t\tcount -= map[word.charAt(i) - \'a\']++ == k ? 1 : 0;\n\t\t\tif (i >= start + length - 1) {\n\t\t\t\tsum += count == length / k ? 1 : 0;\n\t\t\t\tcount += map[word.charAt(i - length + 1) - \'a\'] == k + 1 ? 1 : 0;\n\t\t\t\tcount -= map[word.charAt(i - length + 1) - \'a\']-- == k ? 1 : 0;\n\t\t\t}\n\t\t}\n\t\treturn sum;\n\t}\n}\n```\n\n![IMG_3726.JPG](https://assets.leetcode.com/users/images/97616a0e-28bb-488c-a279-d8f19e233621_1701581872.9352283.jpeg)\n
['C++', 'Java', 'Python3']