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

• Difficulty Level : Medium
• Last Updated : 27 Apr, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: str = 10?0?11
Output: 1000011
Explanation:
Replacing str[2] = ‘0’ and str[4] = ‘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 approachclass 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 approachusing 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 Stringstr = 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)

My Personal Notes arrow_drop_up