human
Can you provide a code to count possible binary strings of length N without P consecutive 0 s and Q consecutive 1 s and the other steps required given the following:
Count possible binary strings of length N without P consecutive 0 s and Q consecutive 1 s | Function to check if a satisfy the given condition or not.
Stores the length of string.
Stores the previous character of the string.
Stores the count of consecutive equal characters.
Traverse the string.
If current character is equal to the previous character.
If count of consecutive 1 s is more than Q.
If count of consecutive 0 s is more than P.
Reset value of cnt.
If count of consecutive 1 s is more than Q.
If count of consecutive 0 s is more than P.
Function to count all distinct binary strings that satisfy the given condition.
Stores the length of str.
If length of str is N.
If str satisfy the given condition.
If str does not satisfy the given condition.
Append a character '0' at end of str.
Append a character '1' at end of str.
Return total count of binary strings.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to count possible binary strings of length N without P consecutive 0 s and Q consecutive 1 s and the other steps required.
 def checkStr ( str , P , Q ) :
	N = len ( str )
	prev = str [ 0 ]
	cnt = 0
	for i in range ( N ) :
		if ( str [ i ] == prev ) :
			cnt += 1
		else :
			if ( prev == '1' and cnt >= Q ) :
				return False
			if ( prev == '0' and cnt >= P ) :
				return False
			cnt = 1
		prev = str [ i ]
	if ( prev == '1' and cnt >= Q ) :
		return False
	if ( prev == '0' and cnt >= P ) :
		return False
	return True
def cntBinStr ( str , N , P , Q ) :
	lenn = len ( str )
	if ( lenn == N ) :
		if ( checkStr ( str , P , Q ) ) :
			return 1
		return 0
	X = cntBinStr ( str + '0' , N , P , Q )
	Y = cntBinStr ( str + '1' , N , P , Q )
	return X + Y
if __name__ == ' _ _ main _ _ ' :
	N = 5
	P = 2
	Q = 3
	print ( cntBinStr ( " " , N , P , Q ) )