human
Maximum difference of zeros and ones in binary string | Set 2 ( O ( n ) time ) | Returns the length of substring with maximum difference of zeroes and ones in binary string.
traverse a binary string from left to right.
add current value to the current_sum according to the Character if it ' s ▁ ' 0 ' add 1 else -1.
update maximum sum.
return - 1 if string does not contain any zero that means all ones otherwise max_sum.
Driven Program"
How can the above be solved in Python?
assistant
#Here is code to maximum difference of zeros and ones in binary string and the other steps required.
 def findLength ( string , n ) :
	current_sum = 0
	max_sum = 0
	for i in range ( n ) :
		current_sum += ( 1 if string [ i ] == '0' else - 1 )
		if current_sum < 0 :
			current_sum = 0
		max_sum = max ( current_sum , max_sum )
	return max_sum if max_sum else 0
s = "11000010001"
n = 11
print ( findLength ( s , n ) )