Find the maximum occurring character after performing the given operations
Last Updated :
12 Nov, 2021
Given a string str consisting of 0, 1, and *, the task is to find the maximum occurring character out of 0 and 1 after performing the given operations:
- Replace * with 0 where * appears on the left side of the existing 0s in the string.
- Replace * with 1 where * appears on the right side of the existing 1s in the string.
- If any * can be replaced by both 0 and 1, then it remains unchanged.
Note: If the frequency of 0 and 1 is same after performing the given operations then print -1.
Examples:
Input: str = “**0**1***0”
Output: 0
Explanation:
Since 0 can replace the * to its left and 1 can replace the * to its right thus string becomes 000**1***0
Input: str = “0*1”
Output: -1
Explanation:
Both 0 and 1 have the same frequency hence the output is -1.
Approach: The idea to generate the final resultant string and then compare the frequency of 0 and 1. Below are the steps:
- Count the initial frequencies of 0 and 1 in the string and store them in variables say count_0 and count_1.
- Initialize a variable, say prev, as -1. Iterate over the string and check if the current character is *. If so, then continue.
- If it is the first character encountered and is 0 then add all * to count_0 and change prev to current index.
- Otherwise, if the first character is 1 then change prev to current index.
- If the previous character is 1 and the current character is 0 then add half of * in between the characters to 0 and half to 1.
- If the previous character is 0 and the current character is 1 then no * character in between them can be replaced.
- If the previous and current both characters are of the same type then add the count of * to the frequencies.
- Compare the frequencies of 0 and 1 and print the maximum occurring character.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve(string S)
{
int count_0 = 0, count_1 = 0;
int prev = -1;
for ( int i = 0; i < S.length(); i++) {
if (S[i] == '0' )
count_0++;
else if (S[i] == '1' )
count_1++;
}
for ( int i = 0; i < S.length(); i++) {
if (S[i] == '*' )
continue ;
else if (S[i] == '0' && prev == -1) {
count_0 = count_0 + i;
prev = i;
}
else if (S[i] == '1' && prev == -1) {
prev = i;
}
else if (S[prev] == '1' && S[i] == '0' ) {
count_0 = count_0 + (i - prev - 1) / 2;
count_1 = count_1 + (i - prev - 1) / 2;
prev = i;
}
else if (S[prev] == '1' && S[i] == '1' ) {
count_1 = count_1 + (i - prev - 1);
prev = i;
}
else if (S[prev] == '0' && S[i] == '1' )
prev = i;
else if (S[prev] == '0' && S[i] == '0' ) {
count_0 = count_0 + (i - prev - 1);
prev = i;
}
}
if (count_0 > count_1)
cout << "0" ;
else if (count_1 > count_0)
cout << "1" ;
else {
cout << -1;
}
}
int main()
{
string str = "**0**1***0" ;
solve(str);
return 0;
}
|
Java
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);
}
}
|
Python3
def solve(S):
count_0 = 0
count_1 = 0
prev = - 1
for i in range ( len (S)) :
if (S[i] = = '0' ):
count_0 + = 1
elif (S[i] = = '1' ):
count_1 + = 1
for i in range ( len (S)):
if (S[i] = = '*' ):
continue
elif (S[i] = = '0' and prev = = - 1 ):
count_0 = count_0 + i
prev = i
elif (S[i] = = '1' and prev = = - 1 ):
prev = i
elif (S[prev] = = '1' and S[i] = = '0' ):
count_0 = count_0 + (i - prev - 1 ) / 2
count_1 = count_1 + (i - prev - 1 ) / / 2
prev = i
elif (S[prev] = = '1' and S[i] = = '1' ):
count_1 = count_1 + (i - prev - 1 )
prev = i
elif (S[prev] = = '0' and S[i] = = '1' ):
prev = i
elif (S[prev] = = '0' and S[i] = = '0' ):
count_0 = count_0 + (i - prev - 1 )
prev = i
if (count_0 > count_1):
print ( "0" )
elif (count_1 > count_0):
print ( "1" )
else :
print ( "-1" )
str = "**0**1***0"
solve( str )
|
C#
using System;
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[i] == '0' )
count_0++;
else if (S[i] == '1' )
count_1++;
}
for ( int i = 0; i < S.Length; i++)
{
if (S[i] == '*' )
continue ;
else if (S[i] == '0' && prev == -1)
{
count_0 = count_0 + i;
prev = i;
}
else if (S[i] == '1' && prev == -1)
{
prev = i;
}
else if (S[prev] == '1' && S[i] == '0' )
{
count_0 = count_0 + (i - prev - 1) / 2;
count_1 = count_1 + (i - prev - 1) / 2;
prev = i;
}
else if (S[prev] == '1' && S[i] == '1' )
{
count_1 = count_1 + (i - prev - 1);
prev = i;
}
else if (S[prev] == '0' && S[i] == '1' )
prev = i;
else if (S[prev] == '0' && S[i] == '0' )
{
count_0 = count_0 + (i - prev - 1);
prev = i;
}
}
if (count_0 > count_1)
Console.Write( "0" );
else if (count_1 > count_0)
Console.Write( "1" );
else
{
Console.Write( "-1" );
}
}
public static void Main ()
{
string str = "**0**1***0" ;
solve(str);
}
}
|
Javascript
<script>
function solve( S) {
var count_0 = 0, count_1 = 0;
var prev = -1;
for (i = 0; i < S.length; i++) {
if (S.charAt(i) == '0' )
count_0++;
else if (S.charAt(i) == '1' )
count_1++;
}
for (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)
document.write( "0" );
else if (count_1 > count_0)
document.write( "1" );
else {
document.write( "-1" );
}
}
var str = "**0**1***0" ;
solve(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...