Java O(N) time and O(1) space solution with explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nThere are three Hints/Intuition here:\n- There are at most 26 different lengths of the 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThere are three Hints/Intuition here:\n- There are at most 26 different lengths of the complete substrings: k * 1, k * 2, \u2026 k * 26\n- For each length, we can use sliding window to count the frequency of each letter in the window.\n- To make sure all the adjacent characteres in a window is differ by at most 2, we just need to keep track a lastOffendingIndex when we change the window and make sure start of the window is greater than that index.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor all possible windows size (Math.min(word.length() / k, 26)), slide the window and check if the substring is valid. \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(26N) -> O(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n\n# Code\n```\nclass Solution {\n    public int countCompleteSubstrings(String word, int k) {\n        int res = 0; \n\n        // Iterate over possible window sizes, limited by the length of the word and the alphabet size\n        for (int wsm = 1; wsm <= Math.min(word.length() / k, 26); wsm++) {\n            int[] freq = new int[26];\n            int lastOffendingIndex = -1;\n            int windowSize = wsm * k;\n            int end = 0; \n\n            for (int start = 0; start <= word.length() - windowSize; start++) {\n                // Expand the window until it reaches the desired size\n                while (end - start < windowSize) {\n                    char c = word.charAt(end);\n                    freq[c - \'a\']++;\n\n                    // Check if the current character violates the adjacent character difference rule\n                    if (end > 0 && windowSize > 1 && Math.abs(word.charAt(end) - word.charAt(end - 1)) > 2) {\n                        lastOffendingIndex = end;\n                    }\n                    end++;\n                }\n\n                // Check if the substring starting from \'start\' is valid\n                if (start >= lastOffendingIndex) {\n                    boolean differenceValid = true;\n                    for (int x : freq) {\n                        if (x != k && x != 0) {\n                            differenceValid = false;\n                            break;\n                        }\n                    }\n                    if (differenceValid) res++;\n                }\n\n                // Decrease the frequency of the starting character as the window shifts right\n                freq[word.charAt(start) - \'a\']--;\n            }\n        }\n        return res;\n    }\n}\n\n```
['Java']