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++ filter_none edit

close play_arrow link

brightness_4

code #include<bits/stdc++.h> using namespace std; int countkDist(string str, int k) { int n = str.length(); int res = 0; int cnt[26]; for ( int i = 0; i < n; i++) { int dist_count = 0; memset (cnt, 0, sizeof (cnt)); for ( int j=i; j<n; j++) { if (cnt[str[j] - 'a' ] == 0) dist_count++; cnt[str[j] - 'a' ]++; if (dist_count == k) res++; if (dist_count > k) break ; } } return res; } int main() { string str = "abcbaa" ; int k = 3; cout << "Total substrings with exactly " << k << " distinct characters :" << countkDist(str, k) << endl; return 0; } chevron_right filter_none Java filter_none edit

close play_arrow link

brightness_4

code import java.util.Arrays; public class CountKSubStr { int countkDist(String str, int k) { int res = 0 ; int n = str.length(); int cnt[] = new int [ 26 ]; for ( int i = 0 ; i < n; i++) { int dist_count = 0 ; Arrays.fill(cnt, 0 ); for ( int j=i; j<n; j++) { if (cnt[str.charAt(j) - 'a' ] == 0 ) dist_count++; cnt[str.charAt(j) - 'a' ]++; if (dist_count == k) res++; } } return res; } 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)); } } chevron_right filter_none Python 3 filter_none edit

close play_arrow link

brightness_4

code def countkDist(str1, k): n = len (str1) res = 0 cnt = [ 0 ] * 27 for i in range ( 0 , n): dist_count = 0 cnt = [ 0 ] * 27 for j in range (i, n): if (cnt[ ord (str1[j]) - 97 ] = = 0 ): dist_count + = 1 cnt[ ord (str1[j]) - 97 ] + = 1 if (dist_count = = k): res + = 1 if (dist_count > k): break return res if __name__ = = "__main__" : str1 = "abcbaa" k = 3 print ( "Total substrings with exactly" , k, "distinct characters : " , end = "") print (countkDist(str1, k)) chevron_right filter_none C# filter_none edit

close play_arrow link

brightness_4

code using System; public class CountKSubStr { int countkDist( string str, int k) { int res = 0; int n = str.Length; int [] cnt = new int [26]; for ( int i = 0; i < n; i++) { int dist_count = 0; Array.Clear(cnt, 0,cnt.Length); for ( int j=i; j<n; j++) { if (cnt[str[j] - 'a' ] == 0) dist_count++; cnt[str[j] - 'a' ]++; if (dist_count == k) res++; } } return res; } 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)); } } chevron_right filter_none PHP filter_none edit

close play_arrow link

brightness_4

code <?php function countkDist( $str , $k ) { $res = 0; $n = strlen ( $str ); $cnt = array (); for ( $i = 0; $i < $n ; $i ++) { $dist_count = 0; $cnt = array_fill (0, 0, true); for ( $j = $i ; $j < $n ; $j ++) { if ( $cnt [ord( $str [ $j ]) - ord( 'a' )] == 0) $dist_count ++; $cnt [ord( $str [ $j ]) - ord( 'a' )]++; if ( $dist_count == $k ) $res ++; } } return $res ; } { $ch = "abcbaa" ; $k = 3; echo ( "Total substrings with exactly " . $k . " distinct characters : " . countkDist( $ch , $k )); } chevron_right filter_none

Total substrings with exactly 3 distinct characters : 8

Time Complexity : O(n*n)

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 contribute.geeksforgeeks.org or mail your article to contribute@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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.