Check if count of 1 s can be made greater in a Binary string by changing 0 s adjacent to 1 s | Function to check whether in a given binary string can we make number of 1 ' s ▁ greater ▁ than ▁ the ▁ number ▁ of ▁ 0' s by doing the given operation ; Stores the count of 0 's ; Stores the count of 1 's ; Traverse through the string S ; Check current character is 1 ; Update cnt1 ; Update cnt0 ; Traverse through the string S ; Check curretn character is 1 ; Check if left adjacent character is 0 ; Change the left adjacent character to _ ; Update the cnt0 ; Check if right adjacent character is 0 ; Change the right adjacent character to _ ; Update the cnt0 ; Check count of 1 ' s ▁ is ▁ greater ▁ ▁ than ▁ the ▁ count ▁ of ▁ 0' s ; Driver Code
def isOnesGreater ( S , N ) : NEW_LINE INDENT S = list ( S ) NEW_LINE cnt0 = 0 NEW_LINE cnt1 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT if ( i > 0 and S [ i - 1 ] == '0' ) : NEW_LINE INDENT S [ i - 1 ] = ' _ ' NEW_LINE cnt0 -= 1 NEW_LINE DEDENT elif ( i < N and S [ i + 1 ] == '0' ) : NEW_LINE INDENT S [ i + 1 ] = ' _ ' NEW_LINE cnt0 -= 1 NEW_LINE DEDENT DEDENT DEDENT if ( cnt1 > cnt0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT S = "01" NEW_LINE N = len ( S ) NEW_LINE isOnesGreater ( S , N ) NEW_LINE