Count number of substrings with exactly k distinct characters - GeeksforGeeks

22 Apr, 2022
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that has exactly k distinct characters. Examples:  
Input: abc, k = 2
Output: 2
Possible substrings are {"ab", "bc"}

Input: aba, k = 2
Output: 3
Possible substrings are {"ab", "ba", "aba"}

Input: aa, k = 1
Output: 3
Possible substrings are {"a", "a", "aa"}
 
 
Method 1 (Brute Force)
If the length of string is n, then there can be n*(n+1)/2 possible substrings. A simple way is to generate all the substring and check each one whether it has exactly k unique characters or not. If we apply this brute force, it would take O(n*n) to generate all substrings and O(n) to do a check on each one. Thus overall it would go O(n*n*n). 
Method 2
The problem can be solved in O(n*n). Idea is to maintain a hash table while generating substring and checking the number of unique characters using that hash table. The implementation below assume that the input string contains only characters from ‘a’ to ‘z’. Implementation  
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to count number of substrings with// exactly k distinct characters in a given string#include<bits/stdc++.h>using namespace std; // Function to count number of substrings// with exactly k unique charactersint countkDist(string str, int k){    int n = str.length();     // Initialize result    int res = 0;     // To store count of characters from 'a' to 'z'    int cnt[26];     // Consider all substrings beginning with    // str[i]    for (int i = 0; i < n; i++)    {        int dist_count = 0;         // Initializing array with 0        memset(cnt, 0, sizeof(cnt));         // Consider all substrings between str[i..j]        for (int j=i; j<n; j++)        {            // If this is a new character for this            // substring, increment dist_count.            if (cnt[str[j] - 'a'] == 0)                dist_count++;             // Increment count of current character            cnt[str[j] - 'a']++;             // If distinct character count becomes k,            // then increment result.            if (dist_count == k)                res++;            if(dist_count > k) break;        }    }     return res;} // Driver Programint main(){    string str = "abcbaa";    int k = 3;    cout << "Total substrings with exactly "         << k <<" distinct characters :"         << countkDist(str, k) << endl;    return 0;}
// Java program to CountKSubStr number of substrings// with exactly distinct characters in a given stringimport java.util.Arrays; public class CountKSubStr{    // Function to count number of substrings    // with exactly k unique characters    int countkDist(String str, int k)    {        // Initialize result        int res = 0;         int n = str.length();         // To store seen characters from 'a' to 'z'        boolean seen[] = new boolean[26];         // Consider all substrings beginning with        // str[i]        for (int i = 0; i < n; i++)        {            int distCount = 0;             // mark all chars as unseen            Arrays.fill(seen, false);             // Consider all substrings between str[i..j]            for (int j=i; j<n; j++)            {                // If this is a new character for this                // substring, increment dist_count.                if (!seen[str.charAt(j) - 'a'])                    distCount++;                 // mark current char as seen                seen[str.charAt(j) - 'a'] = true;                 // If distinct character count becomes k,                // then increment result.                if (distCount == k)                    res++;            }        }         return res;    }     // Driver Program    public static void main(String[] args)    {        CountKSubStr ob = new CountKSubStr();        String ch = "abcbaa";        int k = 3;        System.out.println("Total substrings with exactly " +                           k +    " distinct characters : "                           + ob.countkDist(ch, k));    }}
# Python3 program to count number of# substrings with exactly k distinct# characters in a given string # Function to count number of substrings# with exactly k unique charactersdef countkDist(str1, k):    n = len(str1)         # Initialize result    res = 0     # To store count of characters from    # 'a' to 'z'    cnt = [0] * 27     # Consider all substrings beginning    # with str[i]    for i in range(0, n):        dist_count = 0         # Initializing array with 0        cnt = [0] * 27         # Consider all substrings between str[i..j]        for j in range(i, n):                         # If this is a new character for this            # substring, increment dist_count.            if(cnt[ord(str1[j]) - 97] == 0):                dist_count += 1             # Increment count of current character            cnt[ord(str1[j]) - 97] += 1             # If distinct character count becomes k,            # then increment result.            if(dist_count == k):                res += 1            if(dist_count > k):                break     return res     # Driver Codeif __name__ == "__main__":    str1 = "abcbaa"    k = 3    print("Total substrings with exactly", k,           "distinct characters : ", end = "")    print(countkDist(str1, k)) # This code is contributed by# Sairahul Jella
// C# program to CountKSubStr number of substrings// with exactly distinct characters in a given string   using System;public class CountKSubStr{    // Function to count number of substrings    // with exactly k unique characters    int countkDist(string str, int k)    {        // Initialize result        int res = 0;          int n = str.Length;          // To store count of characters from 'a' to 'z'        int[] cnt = new int[26];          // Consider all substrings beginning with        // str[i]        for (int i = 0; i < n; i++)        {            int dist_count = 0;              // Initializing count array with 0            Array.Clear(cnt, 0,cnt.Length);              // Consider all substrings between str[i..j]            for (int j=i; j<n; j++)            {                // If this is a new character for this                // substring, increment dist_count.                if (cnt[str[j] - 'a'] == 0)                    dist_count++;                  // Increment count of current character                cnt[str[j] - 'a']++;                  // If distinct character count becomes k,                // then increment result.                if (dist_count == k)                    res++;            }        }          return res;    }      // Driver Program    public static void Main()    {        CountKSubStr ob = new CountKSubStr();        string ch = "abcbaa";        int k = 3;        Console.Write("Total substrings with exactly " +                           k +    " distinct characters : "                           + ob.countkDist(ch, k));    }}
<?php// PHP program to CountKSubStr number of substrings// with exactly distinct characters in a given string     // Function to count number of substrings    // with exactly k unique characters    function countkDist($str, $k)    {        // Initialize result        $res = 0;         $n = strlen($str);         // To store count of characters from 'a' to 'z'        $cnt = array();         // Consider all substrings beginning with        // str[i]        for ($i = 0; $i < $n; $i++)        {            $dist_count = 0;             // Initializing count array with 0            $cnt = array_fill(0, 0, true);             // Consider all substrings between str[i..j]            for ($j = $i; $j < $n; $j++)            {                // If this is a new character for this                // substring, increment dist_count.                if ($cnt[ord($str[$j]) - ord('a')] == 0)                    $dist_count++;                 // Increment count of current character                $cnt[ord($str[$j]) - ord('a')]++;                 // If distinct character count becomes k,                // then increment result.                if ($dist_count == $k)                    $res++;            }        }         return $res;    }     // Driver code    {        $ch = "abcbaa";        $k = 3;        echo("Total substrings with exactly " .                        $k . " distinct characters : "                        . countkDist($ch, $k));    } // This code is contributed by Code_Mech
<script> // javascript program to CountKSubStr number of substrings// with exactly distinct characters in a given string // Function to count number of substrings// with exactly k unique charactersfunction countkDist(str , k){    // Initialize result    var res = 0;     var n = str.length;     // To store count of characters from 'a' to 'z'    var cnt = Array.from({length: 26}, (_, i) => 0);     // Consider all substrings beginning with    // str[i]    for (i = 0; i < n; i++)    {        var dist_count = 0;        // Consider all substrings between str[i..j]        for (j=i; j<n; j++)        {            // If this is a new character for this            // substring, increment dist_count.            if (cnt[str.charAt(j).charCodeAt(0) - 'a'.charCodeAt(0)] == 0)                dist_count++;             // Increment count of current character            cnt[str.charAt(j).charCodeAt(0) - 'a'.charCodeAt(0)]++;             // If distinct character count becomes k,            // then increment result.            if (dist_count == k)                res++;        }    }     return res;} // Driver Programvar ch = "abcbaa";var k = 3;document.write("Total substrings with exactly " +                   k +    " distinct characters : "                   + countkDist(ch, k));                    // This code contributed by shikhasingrajput </script>
Output:  
Total substrings with exactly 3 distinct characters : 8
Time Complexity : O(n*n)
Space Complexity : O(1) Explanation :  Only 26 size array is used , which can be consider constant space.
Exercise (Further Optimization): The above code resets count array “cnt[]” in every iteration of outer loop. This can be very costly for large alphabet size. Can we modify the above program such that cnt[] is not reset every time? This article is contributed by Rahul Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
bois
ukasp
Sairahul Jella
Code_Mech
shikhasingrajput
iramkhalid24
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python program to check if a string is palindrome or not
Different methods to reverse a string in C/C++
KMP Algorithm for Pattern Searching
Convert string to char array in C++
Array of Strings in C++ (5 Different Ways to Create)
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Reverse words in a given string
Check whether two strings are anagram of each other
Length of the longest substring without repeating characters