Find the maximum occurring character after performing the given operations | Java program for the above approach ; 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 ; Driver code ; Given string ; Function call
import java . io . * ; class GFG { static void solve ( String S ) { int count_0 = 0 , count_1 = 0 ; int prev = - 1 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == '0' ) count_0 ++ ; else if ( S . charAt ( i ) == '1' ) count_1 ++ ; } for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == ' * ' ) continue ; else if ( S . charAt ( i ) == '0' && prev == - 1 ) { count_0 = count_0 + i ; prev = i ; } else if ( S . charAt ( i ) == '1' && prev == - 1 ) { prev = i ; } else if ( S . charAt ( prev ) == '1' && S . charAt ( i ) == '0' ) { count_0 = count_0 + ( i - prev - 1 ) / 2 ; count_1 = count_1 + ( i - prev - 1 ) / 2 ; prev = i ; } else if ( S . charAt ( prev ) == '1' && S . charAt ( i ) == '1' ) { count_1 = count_1 + ( i - prev - 1 ) ; prev = i ; } else if ( S . charAt ( prev ) == '0' && S . charAt ( i ) == '1' ) prev = i ; else if ( S . charAt ( prev ) == '0' && S . charAt ( i ) == '0' ) { count_0 = count_0 + ( i - prev - 1 ) ; prev = i ; } } if ( count_0 > count_1 ) System . out . print ( "0" ) ; else if ( count_1 > count_0 ) System . out . print ( "1" ) ; else { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { String str = " * *0 * * 1 * * *0" ; solve ( str ) ; } }