human
Give me a code to generate all binary strings of length n with sub and the other steps required given the following:
Generate all binary strings of length n with sub | Utility function to print the given binary string.
This function will be called recursively to generate the next bit for given binary string according to its current state.
Base - case : if the generated binary string meets the required length and the pattern "01" appears twice.
nextbit needs to be 0 because each time we call the function recursively , we call 2 times for 2 cases : next bit is 0 or 1 The is to assure that the binary string is printed one time only.
Generate the next bit for str and call recursive.
Assign first bit.
The next generated bit will either be 0 or 1.
If pattern "01" occurrence is < 2.
Set next bit.
If pattern "01" appears then increase the occurrence of pattern.
Else pattern "01" occurrence equals 2.
If previous bit is 0 then next bit cannot be 1.
Otherwise.
Driver code.
Length of the resulting strings must be at least 4.
Generate all binary strings of length n with sub - string "01" appearing twice"
Solution in Python:
assistant
#Here is code to generate all binary strings of length n with sub and the other steps required.
 def printBinStr ( string , length ) :
	for i in range ( 0 , length ) :
		print ( string [ i ] , end = " " )
	print ( )
def generateBinStr ( string , length , currlen , occur , nextbit ) :
	if currlen == length :
		if occur == 2 and nextbit == 0 :
			printBinStr ( string , length )
		return
	if currlen == 0 :
		string [ 0 ] = nextbit
		generateBinStr ( string , length , currlen + 1 , occur , 0 )
		generateBinStr ( string , length , currlen + 1 , occur , 1 )
	else :
		if occur < 2 :
			string [ currlen ] = nextbit
			if string [ currlen - 1 ] == 0 and nextbit == 1 :
				occur += 1
			generateBinStr ( string , length , currlen + 1 , occur , 0 )
			generateBinStr ( string , length , currlen + 1 , occur , 1 )
		else :
			if string [ currlen - 1 ] == 0 and nextbit == 1 :
				return
			else :
				string [ currlen ] = nextbit
				generateBinStr ( string , length , currlen + 1 , occur , 0 )
				generateBinStr ( string , length , currlen + 1 , occur , 1 )
if __name__ == " _ _ main _ _ " :
	n = 5
	if n < 4 :
		print ( - 1 )
	else :
		string = [ None ] * n
		generateBinStr ( string , n , 0 , 0 , 0 )
		generateBinStr ( string , n , 0 , 0 , 1 )