Easy to understand - JAVA O(n)
Intuition\n Describe your first thoughts on how to solve this problem. \nThis is based on sliding window approach. Here we will fix one windowSize and try to fi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is based on sliding window approach. Here we will fix one windowSize and try to find all the complete strings of that size.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(26 * 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        //We know that final complete string can only be in multiple of \'k\', so we create moving window \n        //of multiples eg- k=2, then windowSize=2,4,6,8...\n        int n = word.length();\n        int res = 0;\n        for(int p=1; p <= 26 ; p++){\n            int windowSize = p*k;\n            if(windowSize > n)\n                break;\n            res += findComplete(word, windowSize, k);\n        }\n\n        return res;\n    }\n\n    public int findComplete(String word, int windowSize, int k){\n      //One single iteration of the word using sliding window takes O(n)\n      int start = 0;\n      int misMatchIndex = -1;\n      int[] freq = new int[26];\n      int completeStringCount = 0;\n\n      for(int end=0; end < word.length(); ){\n          //first ensure that start and end are exactly window size, so if size is less we do end++\n          while(end-start+1 <= windowSize){\n              char ch = word.charAt(end);\n              freq[ch-\'a\']++;\n          \n              //check that consecutive characters are not more than 2 char far\n              if(end>0 && windowSize > 1 && Math.abs(word.charAt(end) - word.charAt(end-1)) > 2)\n                misMatchIndex = end;\n\n              end++;  \n          }\n\n          //now we have start and end at correct position\n          //make sure the misMatch character is not inbetween these\n          if(misMatchIndex <= start && isFreqValid(freq,k)){\n            completeStringCount++;  \n          }\n\n          //now we will shift our window to right by increasing start\n          freq[word.charAt(start)-\'a\']--;\n          start++;\n      }\n\n      return completeStringCount;\n    }\n\n    //this checks if all freq are exactly k\n    public boolean isFreqValid(int[] freq, int k){\n      for(int i=0;i<26;i++){\n        if(freq[i]>0 && freq[i]!=k)\n          return false;\n      }\n      return true;\n    }\n}\n\n//    (st)  (end)\n// 0   1   2   3   4   5\n// p   a   a   b   b   x
['Java']