A radio station logs its programming as a single continuous string of lowercase letters, one letter per minute-slot, where each letter is a code identifying which program aired during that minute. The station calls a contiguous block of exactly k minute-slots a clean block if no program code repeats anywhere inside that block -- every one of the k letters in the block is different from every other letter in it.
Given the full log and the block length k, count how many clean blocks occur in the log (a block starting at each possible position counts separately, so overlapping blocks are all counted).
s
k
s is the programming log and k is the block length.
Print a single integer: the number of contiguous substrings of s of length exactly k in which all k characters are pairwise distinct.
1 <= k <= |s| <= 100000s consists only of lowercase English letters a to zExample 1
Input
abcab 3
Expected
3
Explanation
The length-3 windows are 'abc', 'bca', and 'cab'. Each has three distinct letters, so all three are clean blocks, giving a count of 3.
Example 2
Input
aaaa 2
Expected
0
Explanation
The length-2 windows are 'aa', 'aa', and 'aa'. Every window repeats the letter 'a', so none is clean, giving a count of 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →