Java solution sliding window
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nTo solve this, we perfo
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo solve this, we perform sliding window on the string for 26 times. Each time, we chose an character as the start of our sliding window. Then we slid to find the valid strings. We use an arr cnt to record the count of the 26 characters.\n\nOne thing to be noted is that if we encountered a place where the character is larger than the previous by more than two, we need to flush out the counting arr and refind the next character that is our targeted character.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int countCompleteSubstrings(String word, int k) {\n        int res = 0;\n        HashSet<Integer>set = new HashSet<>();\n        \n        for(int i = 0; i<word.length(); i++){\n            set.add(word.charAt(i)-\'a\');\n        }\n        \n        for(int i :set){\n            res+=helper(i,word,k);\n        }\n        return res;\n    }\n    \n    public int helper(int ch, String word, int k){\n        int[] cnt = new int[26];\n        int res = 0;\n        char c = (char)(ch+\'a\');\n        \n        int left = 0;\n        while(word.charAt(left)!=c)left+=1;\n        for(int i = left; i<word.length(); i++){\n            cnt[word.charAt(i)-\'a\']+=1;\n            if(cnt[ch]>k){\n                cnt[word.charAt(left)-\'a\']-=1;\n                left+=1;\n                while(word.charAt(left)!=c){\n                    cnt[word.charAt(left)-\'a\']-=1;\n                    left+=1;\n                }\n            }\n\t\t\t\t\t\tif(i>0 && (word.charAt(i)-word.charAt(i-1)>2||word.charAt(i-1)-word.charAt(i)>2)){\n\t\t\t\t\t\t\t//flush everything;\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tfor(int j = 0; j<26; j++){\n\t\t\t\t\t\t\t\tcnt[j]=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tleft=i;\n\t\t\t\t\t\t\twhile(left<word.length() && word.charAt(left)!=c){\n\t\t\t\t\t\t\t\tleft+=1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif(left<word.length())cnt[word.charAt(left)-\'a\']+=1;\n\t\t\t\t\t\t\ti=left;\n\t\t\t\t\t\t}\n            if(cnt[ch]==k && helper(cnt,ch,k)){\n\t\t\t\t\t\t\t//System.out.println(word.substring(left,i+1));\n              res+=1;  \n            }\n        }\n        return res;\n    }\n    public boolean helper(int[] cnt, int i, int k){\n        int prev = i;\n        for(int j = i-1; j>=0; j--){\n            if(cnt[j]!=0){\n                if(cnt[j]!=k)return false;\n                if(prev-j>2)return false;\n                prev=j;\n            }\n            \n        }\n        prev = i;\n        for(int j = i+1; j<cnt.length; j++){\n            if(cnt[j]!=0){\n                if(cnt[j]!=k)return false;\n                if(j-prev>2)return false;\n                prev=j;\n            }\n        }\n        return true;\n    }\n}\n```
['Java']