human
Longest substring with atmost K characters from the given set of characters | Function to find the longest substring in the string which contains atmost K characters from the given set of characters.
Base Condition.
Count for Characters from set in substring.
Two pointers.
Loop to iterate until right pointer is not equal to N.
Loop to increase the substring length until the characters from set are at most K.
Check if current pointer points a character from set.
If the count of the char is exceeding the limit.
update answer with substring length.
Increment the left pointer until the count is less than or equal to K.
If the character which comes out then decrement the count by 1.
Driver Code.
Construction of set.
Output result"
How can the above be solved in Python?
assistant
#Here is code to longest substring with atmost K characters from the given set of characters and the other steps required.
 def maxNormalSubstring ( P , Q , K , N ) :
	if ( K == 0 ) :
		return 0
	count = 0
	left = 0
	right = 0
	ans = 0
	while ( right < N ) :
		while ( right < N and count <= K ) :
			if ( P [ right ] in Q ) :
				if ( count + 1 > K ) :
					break
				else :
					count += 1
			right += 1
			if ( count <= K ) :
				ans = max ( ans , right - left )
		while ( left < right ) :
			left += 1
			if ( P [ left - 1 ] in Q ) :
				count -= 1
			if ( count < K ) :
				break
	return ans
P = " giraffe "
Q = { chr }
Q . add ( ' a ' )
Q . add ( ' f ' )
Q . add ( ' g ' )
Q . add ( ' r ' )
K = 2
N = len ( P )
print ( maxNormalSubstring ( P , Q , K , N ) )