Most frequent character in a string after replacing all occurrences of X in a Binary String
Last Updated :
28 Sep, 2022
Given a string S of length N consisting of 1, 0, and X, the task is to print the character (‘1’ or ‘0’) with the maximum frequency after replacing every occurrence of X as per the following conditions:
- If the character present adjacently to the left of X is 1, replace X with 1.
- If the character present adjacently to the right of X is 0, replace X with 0.
- If both the above conditions are satisfied, X remains unchanged.
Note: If the frequency of 1 and 0 is the same after replacements, then print X.
Examples:
Input: S = “XX10XX10XXX1XX”
Output: 1
Explanation:
Operation 1: S = “X11001100X1XX”
Operation 2: S = “111001100X1XX”
No further replacements are possible.
Hence, the frequencies of ‘1’ and ‘0’ are 6 and 4 respectively.
Input: S = “0XXX1”
Output: X
Explanation:
Operation 1: S = “00X11”
No further replacements are possible.
Hence, the frequencies of both ‘1’ and ‘0’ are 2.
Approach: The given problem can be solved based on the following observations:
- All the ‘X’s lying between ‘1’ and ‘0’ (e.g. 1XXX0) is of no significance because neither of ‘1’ and ‘0’ can convert it.
- All the ‘X’s lying between ‘0’ and ‘1’ (e.g. 0XXX1) is also of no significance because it will contribute equally to both 1 and 0. Consider any substring of the form “0X….X1”, then after changing the first occurrence of X from the start and the end of the string, the actual frequency of 0 and 1 in the substring remains unchanged.
From the above observations it can be concluded that the result depends upon the following conditions:
- The count of ‘1’ and ‘0’ in the original string.
- The frequency of X that are present between two consecutive 0s or two consecutive 1s, i.e. “0XXX0” and “1XXXX1” respectively.
- The number of continuous ‘X’ which are present at the starting of string and has a right end ‘1’, i.e. “XXXX1…..”.
- The number of continuous ‘X’s which are present at end of the string and has a left end ‘0’ i.e., …..0XXX.
Hence, count the number of 1s and 0s as per the above conditions and print the resultant count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxOccurringCharacter(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) {
cout << "X" << endl;
}
else if (count0 > count1) {
cout << 0 << endl;
}
else
cout << 1 << endl;
}
int main()
{
string S = "XX10XX10XXX1XX" ;
maxOccurringCharacter(S);
}
|
Java
import java.io.*;
class GFG {
public static void
maxOccurringCharacter(String s)
{
int count0 = 0 , count1 = 0 ;
for ( int i = 0 ;
i < s.length(); i++) {
if (s.charAt(i) == '1' ) {
count1++;
}
else if (s.charAt(i) == '0' ) {
count0++;
}
}
int prev = - 1 ;
for ( int i = 0 ;
i < s.length(); i++) {
if (s.charAt(i) == '1' ) {
prev = i;
break ;
}
}
for ( int i = prev + 1 ;
i < s.length(); i++) {
if (s.charAt(i) != 'X' ) {
if (s.charAt(i) == '1' ) {
count1 += i - prev - 1 ;
prev = i;
}
else {
boolean flag = true ;
for ( int j = i + 1 ;
j < s.length();
j++) {
if (s.charAt(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.charAt(i) == '0' ) {
prev = i;
break ;
}
}
for ( int i = prev + 1 ;
i < s.length(); i++) {
if (s.charAt(i) != 'X' ) {
if (s.charAt(i) == '0' ) {
count0 += i - prev - 1 ;
prev = i;
}
else {
boolean flag = true ;
for ( int j = i + 1 ;
j < s.length(); j++) {
if (s.charAt(j) == '0' ) {
prev = j;
flag = false ;
break ;
}
}
if (!flag) {
i = prev;
}
else {
i = s.length();
}
}
}
}
if (s.charAt( 0 ) == 'X' ) {
int count = 0 ;
int i = 0 ;
while (s.charAt(i) == 'X' ) {
count++;
i++;
}
if (s.charAt(i) == '1' ) {
count1 += count;
}
}
if (s.charAt(s.length() - 1 )
== 'X' ) {
int count = 0 ;
int i = s.length() - 1 ;
while (s.charAt(i) == 'X' ) {
count++;
i--;
}
if (s.charAt(i) == '0' ) {
count0 += count;
}
}
if (count0 == count1) {
System.out.println( "X" );
}
else if (count0 > count1) {
System.out.println( 0 );
}
else
System.out.println( 1 );
}
public static void main(String[] args)
{
String S = "XX10XX10XXX1XX" ;
maxOccurringCharacter(S);
}
}
|
Python3
def maxOccurringCharacter(s):
count0 = 0
count1 = 0
for i in range ( len (s)):
if (s[i] = = '1' ) :
count1 + = 1
elif (s[i] = = '0' ) :
count0 + = 1
prev = - 1
for i in range ( len (s)):
if (s[i] = = '1' ) :
prev = i
break
for i in range (prev + 1 , len (s)):
if (s[i] ! = 'X' ) :
if (s[i] = = '1' ) :
count1 + = i - prev - 1
prev = i
else :
flag = True
for j in range (i + 1 , len (s)):
if (s[j] = = '1' ) :
flag = False
prev = j
break
if (flag = = False ) :
i = prev
else :
i = len (s)
prev = - 1
for i in range ( 0 , len (s)):
if (s[i] = = '0' ) :
prev = i
break
for i in range (prev + 1 , len (s)):
if (s[i] ! = 'X' ) :
if (s[i] = = '0' ) :
count0 + = i - prev - 1
prev = i
else :
flag = True
for j in range (i + 1 , len (s)):
if (s[j] = = '0' ) :
prev = j
flag = False
break
if (flag = = False ) :
i = prev
else :
i = len (s)
if (s[ 0 ] = = 'X' ) :
count = 0
i = 0
while (s[i] = = 'X' ) :
count + = 1
i + = 1
if (s[i] = = '1' ) :
count1 + = count
if (s[( len (s) - 1 )]
= = 'X' ) :
count = 0
i = len (s) - 1
while (s[i] = = 'X' ) :
count + = 1
i - = 1
if (s[i] = = '0' ) :
count0 + = count
if (count0 = = count1) :
print ( "X" )
elif (count0 > count1) :
print ( 0 )
else :
print ( 1 )
S = "XX10XX10XXX1XX"
maxOccurringCharacter(S)
|
C#
using System;
public class GFG
{
public static void maxOccurringCharacter( 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" ;
maxOccurringCharacter(S);
}
}
|
Javascript
<script>
function maxOccurringCharacter(s)
{
var count0 = 0, count1 = 0;
for ( var i = 0;
i < s.length; i++) {
if (s.charAt(i) == '1' ) {
count1++;
}
else if (s.charAt(i) == '0' ) {
count0++;
}
}
var prev = -1;
for ( var i = 0;
i < s.length; i++) {
if (s.charAt(i) == '1' ) {
prev = i;
break ;
}
}
for ( var i = prev + 1;
i < s.length; i++) {
if (s.charAt(i) != 'X' ) {
if (s.charAt(i) == '1' ) {
count1 += i - prev - 1;
prev = i;
}
else {
flag = true ;
for ( var j = i + 1;
j < s.length;
j++) {
if (s.charAt(j) == '1' ) {
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) == '0' ) {
prev = i;
break ;
}
}
for ( var i = prev + 1;
i < s.length; i++) {
if (s.charAt(i) != 'X' ) {
if (s.charAt(i) == '0' ) {
count0 += i - prev - 1;
prev = i;
}
else {
flag = true ;
for ( var j = i + 1;
j < s.length; j++) {
if (s.charAt(j) == '0' ) {
prev = j;
flag = false ;
break ;
}
}
if (!flag) {
i = prev;
}
else {
i = s.length;
}
}
}
}
if (s.charAt(0) == 'X' ) {
var count = 0;
var i = 0;
while (s.charAt(i) == 'X' ) {
count++;
i++;
}
if (s.charAt(i) == '1' ) {
count1 += count;
}
}
if (s.charAt(s.length - 1)
== 'X' ) {
var count = 0;
var i = s.length - 1;
while (s.charAt(i) == 'X' ) {
count++;
i--;
}
if (s.charAt(i) == '0' ) {
count0 += count;
}
}
if (count0 == count1) {
document.write( "X" );
}
else if (count0 > count1) {
document.write(0);
}
else
document.write(1);
}
var S = "XX10XX10XXX1XX" ;
maxOccurringCharacter(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...