Find the maximum occurring character after performing the given operations | Function to find the maximum occurring character ; Initialize count of zero and one ; Iterate over the given string ; Count the zeros ; Count the ones ; Iterate over the given string ; Check if character is * then continue ; Check if first character after * is X ; Add all * to the frequency of X ; Set prev to the i - th character ; Check if first character after * is Y ; Set prev to the i - th character ; Check if prev character is 1 and current character is 0 ; Half of the * will be converted to 0 ; Half of the * will be converted to 1 ; Check if prev and current are 1 ; All * will get converted to 1 ; No * can be replaced by either 0 or 1 ; Prev becomes the ith character ; Check if prev and current are 0 ; All * will get converted to 0 ; If frequency of 0 is more ; If frequency of 1 is more ; Given string ; Function call
def solve ( S ) : NEW_LINE INDENT count_0 = 0 NEW_LINE count_1 = 0 NEW_LINE prev = - 1 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT elif ( S [ i ] == '1' ) : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT DEDENT for i in range ( len ( S ) ) : NEW_LINE INDENT if ( S [ i ] == ' * ' ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( S [ i ] == '0' and prev == - 1 ) : NEW_LINE INDENT count_0 = count_0 + i NEW_LINE prev = i NEW_LINE DEDENT elif ( S [ i ] == '1' and prev == - 1 ) : NEW_LINE INDENT prev = i NEW_LINE DEDENT elif ( S [ prev ] == '1' and S [ i ] == '0' ) : NEW_LINE INDENT count_0 = count_0 + ( i - prev - 1 ) / 2 NEW_LINE count_1 = count_1 + ( i - prev - 1 ) // 2 NEW_LINE prev = i NEW_LINE DEDENT elif ( S [ prev ] == '1' and S [ i ] == '1' ) : NEW_LINE INDENT count_1 = count_1 + ( i - prev - 1 ) NEW_LINE prev = i NEW_LINE DEDENT elif ( S [ prev ] == '0' and S [ i ] == '1' ) : NEW_LINE INDENT prev = i NEW_LINE DEDENT elif ( S [ prev ] == '0' and S [ i ] == '0' ) : NEW_LINE INDENT count_0 = count_0 + ( i - prev - 1 ) NEW_LINE prev = i NEW_LINE DEDENT DEDENT if ( count_0 > count_1 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT elif ( count_1 > count_0 ) : NEW_LINE INDENT print ( "1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT str = " * *0 * * 1 * * *0" NEW_LINE solve ( str ) NEW_LINE