human
Count of substrings which contains a given character K times | Function to count the number of substrings which contains the character C exactly K times.
left and right counters for characters on both sides of subwindow.
left and right pointer on both sides of subwindow.
Initialize the frequency.
result and Length of string.
initialize the left pointer.
initialize the right pointer.
traverse all the window substrings.
counting the characters on leftSide of subwindow.
counting the characters on rightSide of subwindow.
Add the possible substrings on both sides to result.
Setting the frequency for next subwindow.
reset the left , right counters.
Driver code"
Solution in Python:
assistant
#Here is code to count of substrings which contains a given character K times and the other steps required.
 def countSubString ( s , c , k ) :
	leftCount = 0
	rightCount = 0
	left = 0
	right = 0
	freq = 0
	result = 0
	Len = len ( s )
	while ( s [ left ] != c and left < Len ) :
		left += 1
		leftCount += 1
	right = left + 1
	while ( freq != ( k - 1 ) and ( right - 1 ) < Len ) :
		if ( s [ right ] == c ) :
			freq += 1
		right += 1
	while ( left < Len and ( right - 1 ) < Len ) :
		while ( s [ left ] != c and left < Len ) :
			left += 1
			leftCount += 1
		while ( right < Len and s [ right ] != c ) :
			if ( s [ right ] == c ) :
				freq += 1
			right += 1
			rightCount += 1
		result = ( result + ( leftCount + 1 ) * ( rightCount + 1 ) )
		freq = k - 1
		leftCount = 0
		rightCount = 0
		left += 1
		right += 1
	return result
s = "3123231"
c = '3'
k = 2
print ( countSubString ( s , c , k ) )