human
Minimum length substring with exactly K distinct characters | Function to find minimum length substring having exactly k distinct character ..
Starting index of sliding window ..
Ending index of sliding window ..
To store count of character ..
To store count of distinct character in current sliding window ..
To store length of current sliding window ..
To store minimum length ..
To store starting index of minimum length substring ..
Increment count of current character If this count is one then a new distinct character is found in sliding window ..
If number of distinct characters is is greater than k , then move starting point of sliding window forward , until count is k ..
Remove characters from the beginning of sliding window having count more than 1 to minimize length ..
Compare length with minimum length and update if required ..
Return minimum length substring ..
Driver code"
Solution in Python:

Give me a code to minimum length substring with exactly K distinct characters and the other steps required?
assistant
#Here is code to minimum length substring with exactly K distinct characters and the other steps required.
 def findMinLenStr ( str , k ) :
	n = len ( str )
	st = 0
	end = 0
	cnt = [ 0 ] * 26
	distEle = 0
	currlen = 0
	minlen = n
	startInd = - 1
	while ( end < n ) :
		cnt [ ord ( str [ end ] ) - ord ( ' a ' ) ] += 1
		if ( cnt [ ord ( str [ end ] ) - ord ( ' a ' ) ] == 1 ) :
			distEle += 1
		if ( distEle > k ) :
			while ( st < end and distEle > k ) :
				if ( cnt [ ord ( str [ st ] ) - ord ( ' a ' ) ] == 1 ) :
					distEle -= 1
				cnt [ ord ( str [ st ] ) - ord ( ' a ' ) ] -= 1
				st += 1
		if ( distEle == k ) :
			while ( st < end and cnt [ ord ( str [ st ] ) - ord ( ' a ' ) ] > 1 ) :
				cnt [ ord ( str [ st ] ) - ord ( ' a ' ) ] -= 1
				st += 1
			currlen = end - st + 1
			if ( currlen < minlen ) :
				minlen = currlen
				startInd = st
		end += 1
	return str [ startInd : startInd + minlen ]
if __name__ == " _ _ main _ _ " :
	str = " efecfefd "
	k = 4
	print ( findMinLenStr ( str , k ) )