- Related Questions & Answers
- Maximum count of substrings of length K consisting of same characters in C++
- Program to count number of sublists with exactly k unique elements in Python
- Count substrings with same first and last characters in C++
- Longest Substring with At Most K Distinct Characters in C++
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k in C++
- Number of Substrings Containing All Three Characters in C++
- Find K-Length Substrings With No Repeated Characters in Python
- Distinct Echo Substrings in C++
- Count of substrings of a binary string containing K ones in C++
- Count substrings with each character occurring at most k times in C++
- Count Unique Characters of All Substrings of a Given String in C++
- Count number of substrings with numeric value greater than X in C++
- Count Substrings with equal number of 0s, 1s and 2s in C++
- Count all distinct pairs with difference equal to k in C++
- Shortest path with exactly k Edges

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given a string str[] containing lowercase alphabets only and an integer value k. The goal is to find the number of possible substrings of str that have exactly k distinct elements.

**For Example**

str= ”pqr” k=2

Count of number of substrings with exactly k distinct characters are: 2

The substrings having exactly 2 distinct elements are: “pq”, “qr”.

str= ”stristr” k=4

Count of number of substrings with exactly k distinct characters are: 10

The substrings having exactly 2 distinct elements are: “stri”, “tris”, “rist”, “istr”, “stris”, “trist”, “ristr”, “strist”, “tristr”, “stristr”.

**Approach used in the below program is as follows** −

In this approach we will an array array[26] to store the frequency of english alphabets inside the string str. Now traverse str using two for loops, if for substring, each character is occurring once then increment count of unique characters, at the end of that substring if that count is equal to k then increment count of substrings following the given conditions.

Take a string str as input.

Take an integer k with positive value.

Function substring_k(string str, int length, int k) takes str and k and returns count of the number of substrings with exactly k distinct characters.

Take the initial count as 0.

Take frequency array array[26].

Traverse str using two for loops from i=0 to i<lenght and j=i to j<lenght.

Take temp as count of unique elements in substring str[i to j].

If array[str[j] − 'a']==0 then this character str[j] has occurred first time in this substring. So increment temp.

Now increment count of current character using array[str[j] − 'a']++.

If temp is equal to k then increment count.

If temp is more than k then stop commuting further and break the loop.

At the end of all loops return count as result.

#include<bits/stdc++.h> using namespace std; int substring_k(string str, int length, int k){ int count = 0; int array[26]; for (int i = 0; i < length; i++){ int temp = 0; memset(array, 0, sizeof(array)); for (int j = i; j < length; j++){ if(array[str[j] − 'a'] == 0){ temp++; } array[str[j] − 'a']++; if (temp == k){ count++; } if(temp > k){ break; } } } return count; } int main(){ string str = "abc"; int length = str.length(); int k = 1; cout<<"Count of number of substrings with exactly k distinct characters are: "<<substring_k(str, length, k); return 0; }

If we run the above code it will generate the following output −

Count of number of substrings with exactly k distinct characters are: 3

Advertisements