Clean readability, true O(26^2 * n).
Approach\n\nShow different\n\n# Complexity\n\n- Time complexity: O(26^2n) = O(n)\n\n- Space complexity: O(26) = O(1)\n\n# Code\n\n\nclass Solution {\n public:\n
# Approach\n\nShow different\n\n# Complexity\n\n- Time complexity: $$O(26^2n) = O(n)$$\n\n- Space complexity: $$O(26) = O(1)$$\n\n# Code\n\n```\nclass Solution {\n public:\n  int countCompleteSubstrings(string word, int k) {\n    const int lettersCount =\n        unordered_set<char>(word.begin(), word.end()).size();\n    int ans = 0;\n\n    for (int windowSize = k;\n         windowSize <= k * lettersCount && windowSize <= word.length();\n         windowSize += k)\n      ans += countCompleteStrings(word, k, windowSize);\n\n    return ans;\n  }\n\n private:\n  // Returns the number of complete substrings of `windowSize ` of the `word`.\n  int countCompleteStrings(const string& word, int k, int windowSize) {\n    int res = 0;\n    int countLetters = 0;  // the number of letters in the running substring\n    vector<int> count(26);\n    for (int i = 0; i < word.length(); ++i) {\n      ++count[word[i] - \'a\'];\n      ++countLetters;\n      if (i > 0 && abs(word[i] - word[i - 1]) > 2) {\n        count = vector<int>(26);\n        // Start a new substring starting at word[i].\n        ++count[word[i] - \'a\'];\n        countLetters = 1;\n      }\n      if (countLetters == windowSize + 1) {\n        --count[word[i - windowSize] - \'a\'];\n        --countLetters;\n      }\n      if (countLetters == windowSize)\n        res += ranges::all_of(count,\n                              [k](int freq) { return freq == 0 || freq == k; })\n                   ? 1\n                   : 0;\n    }\n    return res;\n  }\n};\n```
['C++']