human
What is a code to longest subsequence of the form 0 * 1 * 0 * in a binary string and the other steps required given the following:
Longest subsequence of the form 0 * 1 * 0 * in a binary string | Returns length of the longest subsequence of the form 0 * 1 * 0 *.
Precomputing values in three arrays pre_count_0 [ i ] is going to store count of 0 s in prefix str [ 0. . i - 1 ] pre_count_1 [ i ] is going to store count of 1 s in prefix str [ 0. . i - 1 ] post_count_0 [ i ] is going to store count of 0 s in suffix str [ i - 1. . n - 1 ].
string is made up of all 0 s or all 1 s.
Compute result using precomputed values.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to longest subsequence of the form 0 * 1 * 0 * in a binary string and the other steps required.
 def longestSubseq ( s ) :
	n = len ( s )
	pre_count_0 = [ 0 for i in range ( n + 2 ) ]
	pre_count_1 = [ 0 for i in range ( n + 1 ) ]
	post_count_0 = [ 0 for i in range ( n + 2 ) ]
	pre_count_0 [ 0 ] = 0
	post_count_0 [ n + 1 ] = 0
	pre_count_1 [ 0 ] = 0
	for j in range ( 1 , n + 1 ) :
		pre_count_0 [ j ] = pre_count_0 [ j - 1 ]
		pre_count_1 [ j ] = pre_count_1 [ j - 1 ]
		post_count_0 [ n - j + 1 ] = post_count_0 [ n - j + 2 ]
		if ( s [ j - 1 ] == '0' ) :
			pre_count_0 [ j ] += 1
		else :
			pre_count_1 [ j ] += 1
		if ( s [ n - j ] == '0' ) :
			post_count_0 [ n - j + 1 ] += 1
	if ( pre_count_0 [ n ] == n or pre_count_0 [ n ] == 0 ) :
		return n
	ans = 0
	for i in range ( 1 , n + 1 ) :
		for j in range ( i , n + 1 , 1 ) :
			ans = max ( pre_count_0 [ i - 1 ] + pre_count_1 [ j ] - pre_count_1 [ i - 1 ] + post_count_0 [ j + 1 ] , ans )
	return ans
if __name__ == ' _ _ main _ _ ' :
	s = "000011100000"
	print ( longestSubseq ( s ) )