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 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
function maxOccuringCharacter ( s ) { var count0 = 0 , count1 = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) == ' ' ) { count1 ++ ; } else if ( s . charAt ( i ) == ' ' ) { count0 ++ ; } } var prev = - 1 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) == ' ' ) { prev = i ; break ; } } for ( var i = prev + 1 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) != ' ' ) { if ( s . charAt ( i ) == ' ' ) { count1 += i - prev - 1 ; prev = i ; } else { flag = true ; for ( var j = i + 1 ; j < s . length ; j ++ ) { if ( s . charAt ( j ) == ' ' ) { flag = false ; prev = j ; break ; } } if ( ! flag ) { i = prev ; } else { i = s . length ; } } } } prev = - 1 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) == ' ' ) { prev = i ; break ; } } for ( var i = prev + 1 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) != ' ' ) { if ( s . charAt ( i ) == ' ' ) { count0 += i - prev - 1 ; prev = i ; } else { flag = true ; for ( var j = i + 1 ; j < s . length ; j ++ ) { if ( s . charAt ( j ) == ' ' ) { prev = j ; flag = false ; break ; } } if ( ! flag ) { i = prev ; } else { i = s . length ; } } } } if ( s . charAt ( 0 ) == ' ' ) { var count = 0 ; var i = 0 ; while ( s . charAt ( i ) == ' ' ) { count ++ ; i ++ ; } if ( s . charAt ( i ) == ' ' ) { count1 += count ; } } if ( s . charAt ( s . length - 1 ) == ' ' ) { var count = 0 ; var i = s . length - 1 ; while ( s . charAt ( i ) == ' ' ) { count ++ ; i -- ; } if ( s . charAt ( i ) == ' ' ) { count0 += count ; } } if ( count0 == count1 ) { document . write ( " " ) ; } else if ( count0 > count1 ) { document . write ( 0 ) ; } else document . write ( 1 ) ; } var S = " " ; maxOccuringCharacter ( S ) ;