# Count of substrings of length K with exactly K distinct characters

Given string str of the lowercase alphabet and an integer K, the task is to count all substrings of length K which have exactly K distinct characters.

Example:

Input: str = “abcc”, K = 2
Output:
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only two valid substrings exist {“ab”, “bc”}.

Input: str = “aabab”, K = 3
Output:
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters
No substrings of length 3 exist with exactly 3 distinct characters.

Naive approach:

The idea is to generate all substrings of length K and, for each substring count, a number of distinct characters. If the length of a string is N, then there can be N – K + 1 substring of length K. Generating these substrings will require O(N) complexity, and checking each substring requires O(K) complexity, hence making the overall complexity like O(N*K).

## C++

 `// C++ program to find the``// count of k length substrings``// with k distinct characters``// using sliding window``#include ``using` `namespace` `std;` `// Function to return the``// required count of substrings``int` `countSubstrings(string s, ``int` `K)``{``    ``int` `n = s.size();``    ``int` `count = 0;``    ` `      ``// Generate all the subarray of size K``    ``for` `(``int` `i = 0; i < n - K + 1; i++) {``        ``string s1 = s.substr(i, K);``        ``unordered_map<``char``, ``int``> unmap;``        ` `        ``for` `(``auto` `c : s1)``            ``unmap++;``          ` `          ``// Check for any duplicate``        ``if` `(unmap.size() == s1.size())``            ``count++;``    ``}` `    ``return` `count;``}` `// Driver code``int` `main()``{``    ``// string str``    ``string str = ``"aabcdabbcdc"``;` `    ``// integer K``    ``int` `K = 3;` `    ``// Print the count of K length``    ``// substrings with k distinct characters``    ``cout << countSubstrings(str, K) << endl;` `    ``return` `0;``}`

## Java

 `// Java program to find the``// count of k length substrings``// with k distinct characters``// using sliding window` `import` `java.util.HashMap;` `public` `class` `Main {``    ` `    ``static` `int` `count = ``0``;``    ` `// Function to return the``// required count of substrings``static` `int` `countSubstrings(String s, ``int` `K) {``    ``int` `n = s.length();``    ` `    ``// Generate all the subarray of size K``    ``for` `(``int` `i = ``0``; i < n - K + ``1``; i++) {``        ``String s1 = s.substring(i, i + K);``        ``HashMap unmap = ``new` `HashMap<>();` `        ``for` `(``char` `c : s1.toCharArray()) {``            ``unmap.put(c, unmap.getOrDefault(c, ``0``) + ``1``);``        ``}` `        ``// Check for any duplicate``        ``if` `(unmap.size() == K) {``            ``count++;``        ``}``    ``}` `    ``return` `count;``}` `// Driver code``public` `static` `void` `main(String[] args) {``    ``// string str``    ``String str = ``"aabcdabbcdc"``;``    ``// integer K``    ``int` `K = ``3``;``    ` `    ``// Print the count of K length``    ``// substrings with k distinct characters``    ``System.out.println(countSubstrings(str, K));``}` `}` `// This code is contributed by Utkarsh`

## Python3

 `# Python3 program to find the``# count of k length substrings``# with k distinct characters``# using sliding window` `def` `countSubstrings(s: ``str``, K: ``int``) ``-``> ``int``:``    ``n ``=` `len``(s)``    ``count ``=` `0``    ` `    ``# Generate all the subarray of size K``    ``for` `i ``in` `range``(n ``-` `K ``+` `1``):``        ``s1 ``=` `s[i:i``+``K]``        ``unmap ``=` `{}``        ` `        ``for` `c ``in` `s1:``            ``if` `c ``in` `unmap:``                ``unmap ``+``=` `1``            ``else``:``                ``unmap ``=` `1``        ` `        ``# Check for any duplicate``        ``if` `len``(unmap) ``=``=` `len``(s1):``            ``count ``+``=` `1``    ` `    ``return` `count` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ``# string str``    ``str` `=` `"aabcdabbcdc"` `    ``# integer K``    ``K ``=` `3` `    ``# Print the count of K length``    ``# substrings with k distinct characters``    ``print``(countSubstrings(``str``, K))``#This code is contributed by ik_9`

## C#

 `// C# program to find the``// count of k length substrings``// with k distinct characters``// using sliding window` `using` `System;``using` `System.Collections.Generic;` `class` `MainClass {``    ` `    ``static` `int` `count = 0;``    ` `    ``// Function to return the``    ``// required count of substrings``    ``static` `int` `CountSubstrings(``string` `s, ``int` `K) {``        ``int` `n = s.Length;` `        ``// Generate all the subarray of size K``        ``for` `(``int` `i = 0; i < n - K + 1; i++) {``            ``string` `s1 = s.Substring(i, K);``            ``Dictionary<``char``, ``int``> unmap = ``new` `Dictionary<``char``, ``int``>();` `            ``foreach` `(``char` `c ``in` `s1.ToCharArray()) {``                ``if` `(unmap.ContainsKey(c)) {``                    ``unmap++;``                ``} ``else` `{``                    ``unmap.Add(c, 1);``                ``}``            ``}` `            ``// Check for any duplicate``            ``if` `(unmap.Count == K) {``                ``count++;``            ``}``        ``}` `        ``return` `count;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(``string``[] args) {``        ``// string str``        ``string` `str = ``"aabcdabbcdc"``;``        ``// integer K``        ``int` `K = 3;` `        ``// Print the count of K length``        ``// substrings with k distinct characters``        ``Console.WriteLine(CountSubstrings(str, K));``    ``}``}``// This code is contributed by Prajwal Kandekar`

## Javascript

 `// Javascript program to find the``// count of k length substrings``// with k distinct characters``// using sliding window` `// Function to return the``// required count of substrings``function` `countSubstrings(s, K)``{``    ``let n = s.length;``    ``let count = 0;``    ` `      ``// Generate all the subarray of size K``    ``for` `(let i = 0; i < n - K + 1; i++) {``        ``let s1 = s.substring(i, i+K);``        ``let unmap = ``new` `Map();``        ` `        ``for` `(let c of s1)``        ``{``            ``if``(unmap.has(c))``                ``unmap.set(c,unmap.get(c)+1);``            ``else``                ``unmap.set(c, 1);``        ``}``           ``// unmap++;``          ` `          ``// Check for any duplicate``        ``if` `(unmap.size == s1.length)``            ``count++;``    ``}` `    ``return` `count;``}` `// Driver code``    ``// string str``    ``let str = ``"aabcdabbcdc"``;` `    ``// integer K``    ``let K = 3;` `    ``// Print the count of K length``    ``// substrings with k distinct characters``    ``document.write(countSubstrings(str, K));`

Output
```5
```

Time Complexity: O(N*K)
Auxiliary Space: O(K)

## Count of substrings of length K with exactly K distinct characters using Sliding Window Technique:

The idea is to use Window Sliding Technique. Maintain a window of size K and keep a count of all the characters in the window using a HashMap. Traverse through the string reduces the count of the first character of the previous window and adds the frequency of the last character of the current window in the HashMap. If the count of distinct characters in a window of length K is equal to K, increment the answer by 1.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the ``// count of k length substrings ``// with k distinct characters ``// using sliding window ``#include ``using` `namespace` `std; ` `// Function to return the ``// required count of substrings ``int` `countSubstrings(string str, ``int` `K) ``{ ``    ``int` `N = str.size(); ``    ``// Store the count ``    ``int` `answer = 0; ` `    ``// Store the count of ``    ``// distinct characters ``    ``// in every window ``    ``unordered_map<``char``, ``int``> map; ` `    ``// Store the frequency of ``    ``// the first K length substring ``    ``for` `(``int` `i = 0; i < K; i++) { ` `        ``// Increase frequency of ``        ``// i-th character ``        ``map[str[i]]++; ``    ``} ` `    ``// If K distinct characters ``    ``// exist ``    ``if` `(map.size() == K) ``        ``answer++; ` `    ``// Traverse the rest of the ``    ``// substring ``    ``for` `(``int` `i = K; i < N; i++) { ` `        ``// Increase the frequency ``        ``// of the last character ``        ``// of the current substring ``        ``map[str[i]]++; ``        ``// Decrease the frequency ``        ``// of the first character ``        ``// of the previous substring ``        ``map[str[i - K]]--; ` `        ``// If the character is not present ``        ``// in the current substring ``        ``if` `(map[str[i - K]] == 0) { ``            ``map.erase(str[i - K]); ``        ``} ` `        ``// If the count of distinct ``        ``// characters is 0 ``        ``if` `(map.size() == K) { ``            ``answer++; ``        ``} ``    ``} ` `    ``// Return the count ``    ``return` `answer; ``} ` `// Driver code ``int` `main() ``{ ``    ``// string str ``    ``string str = ``"aabcdabbcdc"``; ` `    ``// integer K ``    ``int` `K = 3; ` `    ``// Print the count of K length ``    ``// substrings with k distinct characters ``    ``cout << countSubstrings(str, K) << endl; ` `    ``return` `0; ``} `

## Java

 `// Java program to find the count ``// of k length substrings with k ``// distinct characters using ``// sliding window ``import` `java.util.*; ` `class` `GFG{ ` `// Function to return the ``// required count of substrings ``public` `static` `int` `countSubstrings(String str, ``                                ``int` `K) ``{ ``    ``int` `N = str.length(); ``    ` `    ``// Store the count ``    ``int` `answer = ``0``; ` `    ``// Store the count of ``    ``// distinct characters ``    ``// in every window ``    ``Map map = ``new` `HashMap(); ` `    ``// Store the frequency of ``    ``// the first K length substring ``    ``for``(``int` `i = ``0``; i < K; i++) ``    ``{ ``        ` `        ``// Increase frequency of ``        ``// i-th character ``        ``if` `(map.get(str.charAt(i)) == ``null``) ``        ``{ ``            ``map.put(str.charAt(i), ``1``); ``        ``} ``        ``else``        ``{ ``            ``map.put(str.charAt(i), ``            ``map.get(str.charAt(i)) + ``1``); ``        ``} ``    ``} ` `    ``// If K distinct characters ``    ``// exist ``    ``if` `(map.size() == K) ``        ``answer++; ` `    ``// Traverse the rest of the ``    ``// substring ``    ``for``(``int` `i = K; i < N; i++) ``    ``{ ` `        ``// Increase the frequency ``        ``// of the last character ``        ``// of the current substring ``        ``if` `(map.get(str.charAt(i)) == ``null``) ``        ``{ ``            ``map.put(str.charAt(i), ``1``); ``        ``} ``        ``else``        ``{ ``            ``map.put(str.charAt(i), ``            ``map.get(str.charAt(i)) + ``1``); ``        ``} ``        ` `        ``// Decrease the frequency ``        ``// of the first character ``        ``// of the previous substring ``        ``map.put(str.charAt(i - K), ``        ``map.get(str.charAt(i - K)) - ``1``); ` `        ``// If the character is not present ``        ``// in the current substring ``        ``if` `(map.get(str.charAt(i - K)) == ``0``) ``        ``{ ``            ``map.remove(str.charAt(i - K)); ``        ``} ` `        ``// If the count of distinct ``        ``// characters is 0 ``        ``if` `(map.size() == K) ``        ``{ ``            ``answer++; ``        ``} ``    ``} ` `    ``// Return the count ``    ``return` `answer; ``} ` `// Driver code ``public` `static` `void` `main(String[] args) ``{ ``    ` `    ``// string str ``    ``String str = ``"aabcdabbcdc"``; ` `    ``// integer K ``    ``int` `K = ``3``; ` `    ``// Print the count of K length ``    ``// substrings with k distinct characters ``    ``System.out.println(countSubstrings(str, K)); ``} ``} ` `// This code is contributed by grand_master `

## Python3

 `# Python3 program to find the ``# count of k length substrings ``# with k distinct characters ``# using sliding window ` `# Function to return the ``# required count of substrings ``def` `countSubstrings(``str``, K): ` `    ``N ``=` `len``(``str``) ` `    ``# Store the count ``    ``answer ``=` `0` `    ``# Store the count of ``    ``# distinct characters ``    ``# in every window ``    ``map` `=` `{} ` `    ``# Store the frequency of ``    ``# the first K length substring ``    ``for` `i ``in` `range``(K): ` `        ``# Increase frequency of ``        ``# i-th character ``        ``map``[``str``[i]] ``=` `map``.get(``str``[i], ``0``) ``+` `1``        ` `    ``# If K distinct characters ``    ``# exist ``    ``if` `(``len``(``map``) ``=``=` `K): ``        ``answer ``+``=` `1` `    ``# Traverse the rest of the ``    ``# substring ``    ``for` `i ``in` `range``(K, N): ` `        ``# Increase the frequency ``        ``# of the last character ``        ``# of the current substring ``        ``map``[``str``[i]] ``=` `map``.get(``str``[i], ``0``) ``+` `1``        ` `        ``# Decrease the frequency ``        ``# of the first character ``        ``# of the previous substring ``        ``map``[``str``[i ``-` `K]] ``-``=` `1` `        ``# If the character is not present ``        ``# in the current substring ``        ``if` `(``map``[``str``[i ``-` `K]] ``=``=` `0``): ``            ``del` `map``[``str``[i ``-` `K]] ` `        ``# If the count of distinct ``        ``# characters is 0 ``        ``if` `(``len``(``map``) ``=``=` `K): ``            ``answer ``+``=` `1` `    ``# Return the count ``    ``return` `answer ` `# Driver code ``if` `__name__ ``=``=` `'__main__'``: ``    ` `    ``str` `=` `"aabcdabbcdc"` `    ``# Integer K ``    ``K ``=` `3` `    ``# Print the count of K length ``    ``# substrings with k distinct characters ``    ``print``(countSubstrings(``str``, K)) ` `# This code is contributed by mohit kumar 29 `

## C#

 `// C# program to find the count``// of k length substrings with k ``// distinct characters using ``// sliding window ``using` `System;``using` `System.Collections.Generic; ` `class` `GFG{` `// Function to return the ``// required count of substrings ``public` `static` `int` `countSubstrings(``string` `str,``                                  ``int` `K) ``{ ``    ``int` `N = str.Length;``    ` `    ``// Store the count ``    ``int` `answer = 0; ` `    ``// Store the count of ``    ``// distinct characters ``    ``// in every window ``    ``Dictionary<``char``, ``               ``int``> map = ``new` `Dictionary<``char``, ``                                         ``int``>(); ` `    ``// Store the frequency of ``    ``// the first K length substring ``    ``for``(``int` `i = 0; i < K; i++) ``    ``{ ``        ` `        ``// Increase frequency of ``        ``// i-th character``        ``if``(!map.ContainsKey(str[i]))``        ``{``            ``map[str[i]] = 1;``        ``}``        ``else``        ``{``            ``map[str[i]]++; ``        ``}``    ``} ` `    ``// If K distinct characters ``    ``// exist ``    ``if` `(map.Count == K) ``        ``answer++; ` `    ``// Traverse the rest of the ``    ``// substring ``    ``for``(``int` `i = K; i < N; i++)``    ``{ ``        ` `        ``// Increase the frequency ``        ``// of the last character ``        ``// of the current substring ``        ``if``(!map.ContainsKey(str[i]))``        ``{``            ``map[str[i]] = 1;``        ``}``        ``else``        ``{``            ``map[str[i]]++; ``        ``}``        ` `        ``// Decrease the frequency ``        ``// of the first character ``        ``// of the previous substring``        ``map[str[i - K]]--;` `        ``// If the character is not present ``        ``// in the current substring ``        ``if` `(map[str[i - K]] == 0)``        ``{ ``            ``map.Remove(str[i - K]); ``        ``} ` `        ``// If the count of distinct ``        ``// characters is 0 ``        ``if` `(map.Count == K)``        ``{ ``            ``answer++; ``        ``} ``    ``} ` `    ``// Return the count ``    ``return` `answer; ``} ` `// Driver code ``public` `static` `void` `Main(``string``[] args) ``{ ``    ` `    ``// string str ``    ``string` `str = ``"aabcdabbcdc"``; ` `    ``// integer K ``    ``int` `K = 3; ` `    ``// Print the count of K length ``    ``// substrings with k distinct characters ``    ``Console.Write(countSubstrings(str, K));``} ``}` `// This code is contributed by rutvik_56`

## Javascript

 ``

Output
```5
```

Time Complexity: O(N)
Auxiliary Space: O(K)

Previous
Next