[Javascript] Sliding Window
Solution: Sliding Window\n\nThere can be up to 26 unique characters in a substring, and each character occurs exactly k times.\nFor each number of unique charac
**Solution: Sliding Window**\n\nThere can be up to `26` unique characters in a substring, and each character occurs exactly `k` times.\nFor each number of unique characters, maintain a sliding window of characters where \n  1. The difference between two adjacent characters is at most `2`.\n  2. The length of the window doesn\'t exceed the size the substring is meant to be: `unique characters * k`.\nCount the number of substrings with exactly `unique` unique characters which occur `k` times each.\n\nTime Complexity: `O(26n)` \nSpace Complexity: `O(1)` \n```\nvar countCompleteSubstrings = function(word, k) {\n  let n = word.length, complete = 0;\n  for (let unique = 1; unique <= 26; unique++) {\n    let substrSize = unique * k;\n    let charsWithKFreq = 0;\n    let count = Array(26).fill(0);\n    for (let j = 0, i = 0; j < n; j++) {\n      // diff between adjacent chars > 2, reset the window completely\n      if (j > 0 && diff(word[j], word[j - 1]) > 2) {\n        count = Array(26).fill(0);\n        charsWithKFreq = 0;\n        i = j;\n      }\n      count[word.charCodeAt(j) - 97]++;\n      if (count[word.charCodeAt(j) - 97] === k) charsWithKFreq++;\n      // move left pointer up while window is too big for the substring size\n      while (j - i + 1 > substrSize) {\n        if (count[word.charCodeAt(i) - 97] === k) charsWithKFreq--;\n        count[word.charCodeAt(i) - 97]--;\n        i++;\n      }\n      if (charsWithKFreq === unique) complete++;\n    }\n  }\n  return complete;\n};\n\nfunction diff(a, b) {\n  return Math.abs(a.charCodeAt() - b.charCodeAt());\n}\n```
['JavaScript']