Java | Sliding Window | Easy to understand
Intuition\nSimilar to other shared solution, made easy solution. Needs improvement though.\n\n# Approach\n\n1. Count unique chars in word. Each substring must c
# Intuition\nSimilar to other shared solution, made easy solution. Needs improvement though.\n\n# Approach\n\n1. Count unique chars in word. Each substring must contain atleast one unqiue char * k\n2. For each substring of length derived from step 1 -> this is our window size, check complete substring.\n\n# Complexity\n- Time complexity: \n\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\n    public int countCompleteSubstrings(String word, int k) {\n        int ans = 0;\n        // Count unique chars in word.\n        // each substring must contain atleast one unqiue char * k\n        int total_unique_chars = (int) word.chars().distinct().count();\n        for(int num_unqiue_chars = 1; num_unqiue_chars <= total_unique_chars; num_unqiue_chars++){\n            int substr_len = num_unqiue_chars * k; // This is our substring length or window size\n            if(substr_len > word.length()) break;\n            int[] freq = new int[26];\n            int start = 0, end = substr_len; // our window includes chars from 0 through len - 1\n            for(int i = 0; i < end; i++){\n                freq[word.charAt(i) - \'a\']++;\n            }\n            // Check if window contains complete substring\n            if(isCompleteSubstring(word, freq, k, start, end - 1)){\n                ans++;\n            }\n            // Shrink from left/start and expand window to right/end until end of word.\n            for(;end < word.length(); end++){\n                freq[word.charAt(start++) - \'a\']--;\n                freq[word.charAt(end) - \'a\']++;\n                // Check if sliding window contains complete substring\n                if(isCompleteSubstring(word, freq, k, start, end)){\n                    ans++;\n                }\n            }\n        }\n\n        return ans;\n    }\n\n    public boolean isCompleteSubstring(String word, int[] freq, int k, int start, int end){\n\n        for(int i = 0; i < 26; i++){\n            if(freq[i] > 0 && freq[i] != k){\n                return false;\n            }\n        }\n\n        for(int i = start + 1; i <= end; i++){\n            if(Math.abs((word.charAt(i) - \'a\') - (word.charAt(i - 1) - \'a\')) > 2) return false;\n        }\n        return true;\n    }\n}\n```
['Java']