Most frequent character in a string after replacing all occurrences of X in a Binary String | C # program for the above approach ; 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 string 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 string 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 string 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 , print X ; Otherwise , if count of 1 is greater than count of 0 ; Otherwise , print 0 ; Driver Code
using System ; public class GFG { public static void maxOccuringCharacter ( string s ) { int count0 = 0 , count1 = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) { count1 ++ ; } else if ( s [ i ] == '0' ) { count0 ++ ; } } int prev = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) { prev = i ; break ; } } for ( int i = prev + 1 ; i < s . Length ; i ++ ) { if ( s [ i ] != ' X ' ) { if ( s [ i ] == '1' ) { count1 += i - prev - 1 ; prev = i ; } else { bool flag = true ; for ( int j = i + 1 ; j < s . Length ; j ++ ) { if ( s [ j ] == '1' ) { flag = false ; prev = j ; break ; } } if ( ! flag ) { i = prev ; } else { i = s . Length ; } } } } prev = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '0' ) { prev = i ; break ; } } for ( int i = prev + 1 ; i < s . Length ; i ++ ) { if ( s [ i ] != ' X ' ) { if ( s [ i ] == '0' ) { count0 += i - prev - 1 ; prev = i ; } else { bool flag = true ; for ( int j = i + 1 ; j < s . Length ; j ++ ) { if ( s [ j ] == '0' ) { prev = j ; flag = false ; break ; } } if ( ! flag ) { i = prev ; } else { i = s . Length ; } } } } if ( s [ 0 ] == ' X ' ) { int count = 0 ; int i = 0 ; while ( s [ i ] == ' X ' ) { count ++ ; i ++ ; } if ( s [ i ] == '1' ) { count1 += count ; } } if ( s [ s . Length - 1 ] == ' X ' ) { int count = 0 ; int i = s . Length - 1 ; while ( s [ i ] == ' X ' ) { count ++ ; i -- ; } if ( s [ i ] == '0' ) { count0 += count ; } } if ( count0 == count1 ) { Console . WriteLine ( " X " ) ; } else if ( count0 > count1 ) { Console . WriteLine ( 0 ) ; } else Console . WriteLine ( 1 ) ; } public static void Main ( string [ ] args ) { string S = " XX10XX10XXX1XX " ; maxOccuringCharacter ( S ) ; } }