human
Most frequent character in a string after replacing all occurrences of X in a Binary String | Function to find the most frequent character after replacing X with either '0' or '1' according as per the given conditions.
Store the count of 0 s and 1 s in the S.
Count the frequency of 0 and 1.
If the character is 1.
If the character is 0.
Stores first occurence of 1.
Traverse the to count the number of X between two consecutive 1 s.
If the current character is not X.
If the current character is 1 , add the number of Xs to count1 and set prev to i.
Otherwise.
Find next occurence of 1 in the string.
If it is found , set i to prev.
Otherwise , break out of the loop.
Store the first occurence of 0.
Repeat the same procedure to count the number of X between two consecutive 0 s.
If the current character is not X.
If the current character is 0.
Add the count of Xs to count0.
Set prev to i.
Otherwise.
Find the next occurence of 0 in the string.
If it is found , set i to prev.
Otherwise , break out of the loop.
Count number of X present in the starting of the string as XXXX1 ....
Store the count of X.
Increment count1 by count if the condition is satisfied.
Count the number of X present in the ending of the as ... XXXX0.
Store the count of X.
Increment count0 by count if the condition is satisfied.
If count of 1 is equal to count of 0 , prX.
Otherwise , if count of 1 is greater than count of 0.
Otherwise , pr0.
Driver Code"
How can the above be solved in Python?
 Can you provide a code to most frequent character in a string after replacing all occurrences of X in a Binary String and the other steps required?
assistant
#Here is code to most frequent character in a string after replacing all occurrences of X in a Binary String and the other steps required.
 def maxOccuringCharacter ( s ) :
	count0 = 0
	count1 = 0
	for i in range ( len ( s ) ) :
		if ( s [ i ] == '1' ) :
		count1 += 1
		elif ( s [ i ] == '0' ) :
		count0 += 1
	prev = - 1
	for i in range ( len ( s ) ) :
		if ( s [ i ] == '1' ) :
		prev = i
		break
	for i in range ( prev + 1 , len ( s ) ) :
		if ( s [ i ] != ' X ' ) :
		if ( s [ i ] == '1' ) :
			count1 += i - prev - 1
			prev = i
		else :
			flag = True
			for j in range ( i + 1 , len ( s ) ) :
			if ( s [ j ] == '1' ) :
				flag = False
				prev = j
				break
			if ( flag == False ) :
			i = prev
			else :
			i = len ( s )
	prev = - 1
	for i in range ( 0 , len ( s ) ) :
		if ( s [ i ] == '0' ) :
		prev = i
		break
	for i in range ( prev + 1 , len ( s ) ) :
		if ( s [ i ] != ' X ' ) :
		if ( s [ i ] == '0' ) :
			count0 += i - prev - 1
			prev = i
		else :
			flag = True
			for j in range ( i + 1 , len ( s ) ) :
			if ( s [ j ] == '0' ) :
				prev = j
				flag = False
				break
			if ( flag == False ) :
			i = prev
			else :
			i = len ( s )
	if ( s [ 0 ] == ' X ' ) :
		count = 0
		i = 0
		while ( s [ i ] == ' X ' ) :
		count += 1
		i += 1
		if ( s [ i ] == '1' ) :
		count1 += count
	if ( s [ ( len ( s ) - 1 ) ] == ' X ' ) :
		count = 0
		i = len ( s ) - 1
		while ( s [ i ] == ' X ' ) :
		count += 1
		i -= 1
		if ( s [ i ] == '0' ) :
		count0 += count
	if ( count0 == count1 ) :
		print ( " X " )
	elif ( count0 > count1 ) :
		print ( 0 )
	else :
		print ( 1 )
S = " XX10XX10XXX1XX "
maxOccuringCharacter ( S )