Count Complete Substrings

You are given a string word and an integer k.

A substring s of word is complete if:

  • Each character in s occurs exactly k times.
  • The difference between two adjacent characters is at most 2. That is, for any two adjacent characters c1 and c2 in s, the absolute difference in their positions in the alphabet is at most 2.

Return the number of complete substrings of word.

A substring is a non-empty contiguous sequence of characters in a string.

Example 1
Inputword = "igigee", k = 2
Output3
The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are igig, ee, and igigee.
Example 2
Inputword = "aaabbbccc", k = 3
Output6
The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are aaa, bbb, ccc, aaabbb, bbbccc, and aaabbbccc.

Constraints

  • 1 <= word.length <= 10^5
  • word consists only of lowercase English letters.
  • 1 <= k <= word.length

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate