


'str' = abcad and 'k' = 2.
We can see that the substrings {ab, bc, ca, ad} are the only substrings with 2 distinct characters.
Therefore, the answer will be 4.
The first line contains a string ‘str’.
The second line contains a single integer 'k'.
The output contains a single integer which is the count of all possible substrings having exactly 'k' distinct characters.
You don’t have to print anything. Just implement the given function.
The idea is to check for all the substrings of the given string and then check for each substring whether it contains exactly K different characters or not.
The idea is the same as the previous approach to check for all the substrings of the given string and then check for each substring whether it contains exactly K different characters or not. Here, we will use an array of 26 length to store the count of each character in the substring.
The idea is to use a sliding window to count the number of substrings with at most K different characters because finding substrings with exactly K characters will again cost us N ^ 2 times. Therefore we will count as the number of substrings with exactly K different characters will be the number of substrings with at most K different characters - number of substrings with at most (K-1) different characters.