✅☑[C++/Java/Python/JavaScript] || Maps || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n1. Counting Complete Substrings:\n\n    - The countCompleteSubstrings functio
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n1. **Counting Complete Substrings:**\n\n    - The `countCompleteSubstrings` function iterates through different substring lengths (from 1 to the count of unique characters in the word) to count complete substrings of length `k` that meet specific conditions.\n1. **Sliding Window Approach:**\n\n    - It uses a sliding window approach to check substrings of size `k * subLen` where `subLen` varies from 1 to the count of unique characters.\n    - For each window, it checks if the substring satisfies both frequency and adjacent difference conditions.\n1. **Helper Functions:**\n\n    - `hasEqualFrequency`: Checks if all character frequencies in the window are equal to `k` or `0`.\n    - `hasAdjacentDifferenceAtMostTwo`: Checks if the adjacent character difference in the window is at most two.\n\n\n\n# Complexity\n- *Time complexity:*\n   $$O(26*n)$$\n    \n\n- *Space complexity:*\n   $$O(n)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int countCompleteSubstrings(string word, int k) {\n        int count = 0;\n        int uniqueChars = unordered_set<char>(word.begin(), word.end()).size(); // Count unique characters in the word\n\n        // Loop through substring lengths from 1 to the count of unique characters\n        for (int subLen = 1; subLen <= uniqueChars; ++subLen) {\n            int windowSize = subLen * k; // Define the window size\n\n            unordered_map<char, int> charFreq; // Map to store character frequencies\n            int start = 0;\n            int end = start + windowSize - 1;\n\n            // Populate the character frequency map for the initial window\n            for (int i = start; i <= min(end, static_cast<int>(word.length()) - 1); ++i) {\n                charFreq[word[i]]++;\n            }\n\n            // Sliding window approach\n            while (end < word.length()) {\n                // Check conditions for equal frequency and adjacent difference at most two\n                if (hasEqualFrequency(charFreq, k) && hasAdjacentDifferenceAtMostTwo(word, start, end)) {\n                    count++;\n                }\n\n                charFreq[word[start]]--; // Move the window by decrementing the start character frequency\n                start++;\n                end++;\n\n                // Update the end character frequency if the window size is less than word length\n                if (windowSize < word.length()) {\n                    charFreq[word[end]]++;\n                }\n            }\n        }\n\n        return count;\n    }\n\n    // Function to check if all character frequencies are equal to k or 0\n    bool hasEqualFrequency(unordered_map<char, int>& charFreq, int k) {\n        for (auto& entry : charFreq) {\n            if (entry.second != k && entry.second != 0) {\n                return false;\n            }\n        }\n        return true;\n    }\n\n    // Function to check if adjacent character difference is at most two\n    bool hasAdjacentDifferenceAtMostTwo(string& word, int start, int end) {\n        for (int i = start + 1; i <= end; ++i) {\n            if (abs(word[i] - word[i - 1]) > 2) {\n                return false;\n            }\n        }\n        return true;\n    }\n};\n\n\n\n```\n```C []\n#include <stdio.h>\n#include <string.h>\n\nint hasEqualFrequency(int charFreq[], int size, int k) {\n    for (int i = 0; i < size; ++i) {\n        if (charFreq[i] != k && charFreq[i] != 0) {\n            return 0;\n        }\n    }\n    return 1;\n}\n\nint hasAdjacentDifferenceAtMostTwo(char word[], int start, int end) {\n    for (int i = start + 1; i <= end; ++i) {\n        if (abs(word[i] - word[i - 1]) > 2) {\n            return 0;\n        }\n    }\n    return 1;\n}\n\nint countCompleteSubstrings(char word[], int k) {\n    int count = 0;\n    int uniqueChars = 0;\n    int charFreq[128] = {0}; // Assuming ASCII characters\n\n    // Count unique characters in the word\n    for (int i = 0; word[i] != \'\\0\'; ++i) {\n        if (!charFreq[word[i]]) {\n            uniqueChars++;\n        }\n        charFreq[word[i]]++;\n    }\n\n    // Loop through substring lengths from 1 to the count of unique characters\n    for (int subLen = 1; subLen <= uniqueChars; ++subLen) {\n        int windowSize = subLen * k; // Define the window size\n        int start = 0;\n        int end = start + windowSize - 1;\n\n        // Populate the character frequency map for the initial window\n        for (int i = start; i <= end && word[i] != \'\\0\'; ++i) {\n            charFreq[word[i]]++;\n        }\n\n        // Sliding window approach\n        while (word[end] != \'\\0\') {\n            // Check conditions for equal frequency and adjacent difference at most two\n            if (hasEqualFrequency(charFreq, 128, k) && hasAdjacentDifferenceAtMostTwo(word, start, end)) {\n                count++;\n            }\n\n            charFreq[word[start]]--; // Move the window by decrementing the start character frequency\n            start++;\n            end++;\n\n            // Update the end character frequency if the window size is less than word length\n            if (windowSize < strlen(word)) {\n                charFreq[word[end]]++;\n            }\n        }\n    }\n\n    return count;\n}\n\n\n\n\n\n\n```\n```Java []\nimport java.util.*;\n\npublic class Solution {\n    public int countCompleteSubstrings(String word, int k) {\n        int count = 0;\n        int uniqueChars = new HashSet<>(Arrays.asList(word.split(""))).size();\n\n        for (int subLen = 1; subLen <= uniqueChars; ++subLen) {\n            int windowSize = subLen * k;\n\n            Map<Character, Integer> charFreq = new HashMap<>();\n            int start = 0;\n            int end = start + windowSize - 1;\n\n            for (int i = start; i <= Math.min(end, word.length() - 1); ++i) {\n                charFreq.put(word.charAt(i), charFreq.getOrDefault(word.charAt(i), 0) + 1);\n            }\n\n            while (end < word.length()) {\n                if (hasEqualFrequency(charFreq, k) && hasAdjacentDifferenceAtMostTwo(word, start, end)) {\n                    count++;\n                }\n\n                charFreq.put(word.charAt(start), charFreq.get(word.charAt(start)) - 1);\n                start++;\n                end++;\n\n                if (windowSize < word.length()) {\n                    charFreq.put(word.charAt(end), charFreq.getOrDefault(word.charAt(end), 0) + 1);\n                }\n            }\n        }\n\n        return count;\n    }\n\n    private boolean hasEqualFrequency(Map<Character, Integer> charFreq, int k) {\n        for (int value : charFreq.values()) {\n            if (value != k && value != 0) {\n                return false;\n            }\n        }\n        return true;\n    }\n\n    private boolean hasAdjacentDifferenceAtMostTwo(String word, int start, int end) {\n        for (int i = start + 1; i <= end; ++i) {\n            if (Math.abs(word.charAt(i) - word.charAt(i - 1)) > 2) {\n                return false;\n            }\n        }\n        return true;\n    }\n\n\n\n```\n```python3 []\nclass Solution:\n    def countCompleteSubstrings(self, word, k):\n        count = 0\n        unique_chars = len(set(word))\n\n        for sub_len in range(1, unique_chars + 1):\n            window_size = sub_len * k\n            char_freq = {}\n            start = 0\n            end = start + window_size - 1\n\n            for i in range(start, min(end, len(word))):\n                char_freq[word[i]] = char_freq.get(word[i], 0) + 1\n\n            while end < len(word):\n                if self.has_equal_frequency(char_freq, k) and self.has_adjacent_difference_at_most_two(word, start, end):\n                    count += 1\n\n                char_freq[word[start]] -= 1\n                start += 1\n                end += 1\n\n                if window_size < len(word):\n                    char_freq[word[end]] = char_freq.get(word[end], 0) + 1\n\n        return count\n\n    def has_equal_frequency(self, char_freq, k):\n        for value in char_freq.values():\n            if value != k and value != 0:\n                return False\n        return True\n\n    def has_adjacent_difference_at_most_two(self, word, start, end):\n        for i in range(start + 1, end + 1):\n            if abs(ord(word[i]) - ord(word[i - 1])) > 2:\n                return False\n        return True\n\n\n\n\n```\n```javascript []\nfunction hasEqualFrequency(charFreq, k) {\n    for (let freq of charFreq.values()) {\n        if (freq !== k && freq !== 0) {\n            return false;\n        }\n    }\n    return true;\n}\n\nfunction hasAdjacentDifferenceAtMostTwo(word, start, end) {\n    for (let i = start + 1; i <= end; ++i) {\n        if (Math.abs(word.charCodeAt(i) - word.charCodeAt(i - 1)) > 2) {\n            return false;\n        }\n    }\n    return true;\n}\n\nfunction countCompleteSubstrings(word, k) {\n    let count = 0;\n    const charFreq = new Map();\n    let uniqueChars = 0;\n\n    for (let i = 0; i < word.length; ++i) {\n        if (!charFreq.has(word[i])) {\n            uniqueChars++;\n        }\n        charFreq.set(word[i], (charFreq.get(word[i]) || 0) + 1);\n    }\n\n    for (let subLen = 1; subLen <= uniqueChars; ++subLen) {\n        const windowSize = subLen * k;\n        let start = 0;\n        let end = start + windowSize - 1;\n\n        for (let i = start; i <= end && i < word.length; ++i) {\n            charFreq.set(word[i], (charFreq.get(word[i]) || 0) + 1);\n        }\n\n        while (end < word.length) {\n            if (hasEqualFrequency(charFreq, k) && hasAdjacentDifferenceAtMostTwo(word, start, end)) {\n                count++;\n            }\n\n            charFreq.set(word[start], charFreq.get(word[start]) - 1);\n            start++;\n            end++;\n\n            if (windowSize < word.length) {\n                charFreq.set(word[end], (charFreq.get(word[end]) || 0) + 1);\n            }\n        }\n    }\n\n    return count;\n}\n\n\n\n```\n\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Ordered Map', 'C', 'Sliding Window', 'C++', 'Java', 'Python3', 'JavaScript']