human
Count number of substrings with exactly k distinct characters | Function to count number of substrings with exactly k unique characters.
Initialize result.
To store count of characters from ' a ' to ' z '.
Consider all substrings beginning with str [ i ].
Initializing array with 0.
Consider all substrings between str [ i . . j ].
If this is a new character for this substring , increment dist_count ..
Increment count of current character.
If distinct character count becomes k , then increment result ..
Driver Code"
How can the above be solved in Python?

=====
What is a code to count number of substrings with exactly k distinct characters and the other steps required?
assistant
#Here is code to count number of substrings with exactly k distinct characters and the other steps required.
 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 ) )