Open In App

# Replace ‘?’ to convert given string to a binary string with maximum count of ‘0’ and “10”

Given string str, consisting of three different types of characters ‘0’, ‘1’ and ‘?’, the task is to convert the given string to a binary string by replacing the ‘?’ characters with either ‘0’ or ‘1’ such that the count of 0s and 10 in the binary string is maximum.

Examples:

Input: str = 10?0?11
Output: 1000011
Explanation:
Replacing str = ‘0’ and str = ‘0’ modifies string str = “1000011”.
The count of 0s in the string is 4 and the count of 10 in the string 1 which is the maximum possible count obtained from the given string.

Input: str = 1?1?
Output: 1010

Approach: The idea is to use the fact that replacing ‘?’ character with a ‘0’ character always maximizes the count of 0 and 10. Following are the observation:

If the character ‘?’ is followed by ‘0’ then replacing the character ‘?’ to ‘0’ increment the count of 0s.
If the character ‘1’ is followed by ‘?’ then replacing the character ‘?’ to ‘0’ increment the count of 0s and 10

Follow the below steps to solve the problem:

• Traverse the string and check if the current character is ‘?’ or not. If found to be true, then replace the current character with ‘0’.
• Finally, print the string.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to maximize count of 0 and 10``// by replacing character '?' to '0' or '1'``void` `findMaxOccurence(string str, ``int` `N)``{``    ``// Traverse the given string``    ``for` `(``int` `i = 0; i < N; i++) {``        ` `        ``// If current character``        ``// is '?'``        ``if` `(str[i] == ``'?'``) {``            ` `            ``// Replace str[i] to '0'``            ``str[i] = ``'0'``;``         ``}``    ``}``    ``cout << str <

## Java

 `// Java program to implement``// the above approach``class` `GFG``{` `  ``// Function to maximize count of 0 and 10``  ``// by replacing character '?' to '0' or '1'``  ``static` `void` `findMaxOccurence(``char``[] str, ``int` `N)``  ``{``    ``// Traverse the given String``    ``for` `(``int` `i = ``0``; i < N; i++)``    ``{` `      ``// If current character``      ``// is '?'``      ``if` `(str[i] == ``'?'``)``      ``{` `        ``// Replace str[i] to '0'``        ``str[i] = ``'0'``;``      ``}``    ``}``    ``System.out.print(str);` `  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``// Given String``    ``String str = ``"10?0?11"``;``    ``int` `N = str.length();``    ``findMaxOccurence(str.toCharArray(),N);``  ``}``}` `// This code is contributed by shikhasingrajput`

## C#

 `// C# program to implement``// the above approach``using` `System;``class` `GFG``{` `  ``// Function to maximize count of 0 and 10``  ``// by replacing character '?' to '0' or '1'``  ``static` `void` `findMaxOccurence(``char``[] str, ``int` `N)``  ``{``    ` `    ``// Traverse the given String``    ``for` `(``int` `i = 0; i < N; i++)``    ``{` `      ``// If current character``      ``// is '?'``      ``if` `(str[i] == ``'?'``)``      ``{` `        ``// Replace str[i] to '0'``        ``str[i] = ``'0'``;``      ``}``    ``}``    ``Console.Write(str);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main(String[] args)``  ``{``    ` `    ``// Given String``    ``String str = ``"10?0?11"``;``    ``int` `N = str.Length;``    ``findMaxOccurence(str.ToCharArray(),N);``  ``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to maximize count of 0 and 10``# by replacing character '?' to '0' or '1'``def` `findMaxOccurence(``str``, N) :``  ` `    ``# Traverse the given String``    ``for` `i ``in` `range``(N) :` `        ``# If current character``        ``# is '?'``        ``if` `(``str``[i] ``=``=` `'?'``) :``        ` `            ``# Replace str[i] to '0'``            ``str``[i] ``=` `'0'`       `    ``print``("".join(``str``))` `# Driver Code` `# Given String``str` `=` `list``(``"10?0?11"``)``N ``=` `len``(``str``)``findMaxOccurence(``str``, N)` `# This code is contributed by Dharanendra L V`

## Javascript

 ``

Output:

`1000011`

Time Complexity: O(N), where N is the length of the string
Auxiliary Space: O(1)