human
Can you provide a code to minimum characters required to be removed to sort binary string in ascending order and the other steps required given the following:
Minimum characters required to be removed to sort binary string in ascending order | Function to find the minimum count of characters to be removed to make the string sorted in ascending order.
Length of given string.
Stores the first occurrence of '1.
Stores the last occurrence of '0.
Traverse the string to find the first occurrence of '1.
Traverse the string to find the last occurrence of '0.
Return 0 if the str have only one type of character.
Initialize count1 and count0 to count '1' s before lastIdx0 and '0' s after firstIdx1.
Traverse the string to count0.
Traverse the string to count1.
Return the minimum of count0 and count1.
Given string str.
Function call"
Solution in Python:
assistant
#Here is code to minimum characters required to be removed to sort binary string in ascending order and the other steps required.
 def minDeletion ( s ) :
	n = len ( s )
'
	firstIdx1 = - 1
'
	lastIdx0 = - 1
'
	for i in range ( 0 , n ) :
		if ( str [ i ] == '1' ) :
			firstIdx1 = i
			break
'
	for i in range ( n - 1 , - 1 , - 1 ) :
		if ( str [ i ] == '0' ) :
			lastIdx0 = i
			break
	if ( firstIdx1 == - 1 or lastIdx0 == - 1 ) :
		return 0
	count1 = 0
	count0 = 0
	for i in range ( 0 , lastIdx0 ) :
		if ( str [ i ] == '1' ) :
			count1 += 1
	for i in range ( firstIdx1 + 1 , n ) :
		if ( str [ i ] == '1' ) :
			count0 += 1
	return min ( count0 , count1 )
str = "1000101"
print ( minDeletion ( str ) )