Number of substrings with exactly k distinct characters

In this OpenGenus article, we will be solving Find number of substrings with exactly k distinct characters problem. In this problem, we are given a string and our job is to count the total number of substrings of the given string that contains exactly k distinct characters. If no substring exists that contains exactly k distinct characters then return -1.

For example:

Input: xyz, k = 2Output: 2substrings are {"xy", "yz"}Input: xyx, k = 2Output: 3substrings are {"xy", "yx", "xyx"}Input: z...
 •  0 comments  •  flag
Share on Twitter
Published on May 31, 2021 11:05
No comments have been added yet.