Maximum count of characters that can replace ? by at most A 0s and B 1s with no adjacent duplicates

Given a string S containing only two special characters β€˜*β€˜ and β€˜?β€˜, and two integers A and B that denotes the count of available 0’s and 1’s. The task is to count the maximum number of characters that can be placed in the place of β€˜?β€˜ such that no two adjacent characters are the same.

Examples:

Input: s = β€œ*???*???*”, A = 4, B = 3
Output: 6
Explanation: The string can be modified to β€œ*010*010*”. Therefore, maximum number of characters that can be placed in the place of β€˜?’ are (4 + 2 = 6)
 

Input: s = β€œ???*??*”, A = 0, B = 5
Output: 3
Explanation: The string can be modified to β€œ*1?1*?1*”. Therefore, maximum number of characters that can be placed in the place of β€˜?’ are (0 + 3 = 3)

 

Approach: The task can be solved by keeping track of contiguous segments of β€˜?’s and placing 0’s and 1’s such that after replacing β€˜?’s, no two adjacent elements in the resultant string are the same.

Follow the below steps to solve the problem:

  • Initialize a vector β€˜v’, which will store the lengths of contiguous segments of ?s
  • The variable β€˜cur’ stores the current count of ?s, as soon as a β€˜*’ is encountered, store the value of cur inside v
  • Now, start iterating the stored segment lengths of ?s, and greedily assign 0s and 1s in-place of ?s
  • Keep track of the maximum number of characters that can be placed in the place of ?s

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the maximum number
// of characters 'A' and 'B' that can be
// placed in position of '?'
int maximumChar(string s, int A, int B)
{
    // Length of the string
    int len = s.size();
 
    // Store the current count of '?'s
    int curr = 0;
 
    // Store the lengths of contiguous
    // segments of '?'s
    vector<int> v;
 
    // Traversing the string
    for (int i = 0; i < len; i++) {
        // If character is '?'
        // increment curr by 1
        if (s[i] == '?') {
            curr++;
        }
 
        // If character is '*'
        else {
            // If curr is not equal to 0
            if (curr != 0) {
                v.push_back(curr);
 
                // Re-initialise curr to 0
                curr = 0;
            }
        }
    }
 
    // After traversing the string
    // if curr is not equal to 0
    if (curr != 0) {
        v.push_back(curr);
    }
 
    // Variable for maximum count
    int count = 0;
 
    // Traversing the vector
    for (int i = 0; i < v.size(); i++) {
        // Variable to store half of
        // each elements in vector
        int x = v[i] / 2;
 
        // Variable to store half of
        // each elements with its remainder
        int y = v[i] / 2 + v[i] % 2;
 
        // If A is greater than B
        if (A > B) {
            // Swap both
            swap(A, B);
        }
 
        // Increment count by
        // minimum of A and x
        count += min(A, x);
 
        // Update A
        A -= min(A, x);
 
        // Increment count by
        // minimum of B and y
        count += min(B, y);
 
        // Update B
        B -= min(B, y);
    }
 
    // Return count
    return count;
}
 
// Driver Code
int main()
{
    string s = "*???*???*";
    int A = 4, B = 3;
 
    cout << maximumChar(s, A, B);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.ArrayList;
 
class GFG {
 
    // Function to count the maximum number
    // of characters 'A' and 'B' that can be
    // placed in position of '?'
    public static int maximumChar(String s, int A, int B)
    {
       
        // Length of the string
        int len = s.length();
 
        // Store the current count of '?'s
        int curr = 0;
 
        // Store the lengths of contiguous
        // segments of '?'s
        ArrayList<Integer> v = new ArrayList<Integer>();
 
        // Traversing the string
        for (int i = 0; i < len; i++) {
            // If character is '?'
            // increment curr by 1
            if (s.charAt(i) == '?') {
                curr++;
            }
 
            // If character is '*'
            else {
                // If curr is not equal to 0
                if (curr != 0) {
                    v.add(curr);
 
                    // Re-initialise curr to 0
                    curr = 0;
                }
            }
        }
 
        // After traversing the string
        // if curr is not equal to 0
        if (curr != 0) {
            v.add(curr);
        }
 
        // Variable for maximum count
        int count = 0;
 
        // Traversing the vector
        for (int i = 0; i < v.size(); i++)
        {
           
            // Variable to store half of
            // each elements in vector
            int x = v.get(i) / 2;
 
            // Variable to store half of
            // each elements with its remainder
            int y = v.get(i) / 2 + v.get(i) % 2;
 
            // If A is greater than B
            if (A > B)
            {
               
                // Swap both
                int temp = A;
                A = B;
                B = temp;
            }
 
            // Increment count by
            // minimum of A and x
            count += Math.min(A, x);
 
            // Update A
            A -= Math.min(A, x);
 
            // Increment count by
            // minimum of B and y
            count += Math.min(B, y);
 
            // Update B
            B -= Math.min(B, y);
        }
 
        // Return count
        return count;
    }
 
    // Driver Code
    public static void main(String args[]) {
        String s = "*???*???*";
        int A = 4, B = 3;
 
        System.out.println(maximumChar(s, A, B));
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3




# python program for the above approach
import math
 
# Function to count the maximum number
# of characters 'A' and 'B' that can be
# placed in position of '?'
def maximumChar(s, A, B):
 
        # Length of the string
    le = len(s)
 
    # Store the current count of '?'s
    curr = 0
 
    # Store the lengths of contiguous
    # segments of '?'s
    v = []
 
    # Traversing the string
    for i in range(0, le):
                # If character is '?'
                # increment curr by 1
        if (s[i] == '?'):
            curr += 1
 
            # If character is '*'
        else:
                        # If curr is not equal to 0
            if (curr != 0):
                v.append(curr)
 
                # Re-initialise curr to 0
                curr = 0
 
        # After traversing the string
        # if curr is not equal to 0
    if (curr != 0):
        v.append(curr)
 
        # Variable for maximum count
    count = 0
 
    # Traversing the vector
    for i in range(0, len(v)):
                # Variable to store half of
                # each elements in vector
        x = v[i] // 2
 
        # Variable to store half of
        # each elements with its remainder
        y = v[i] // 2 + v[i] % 2
 
        # If A is greater than B
        if (A > B):
                        # Swap both
            temp = A
            A = B
            B = temp
 
            # Increment count by
            # minimum of A and x
        count += min(A, x)
 
        # Update A
        A -= min(A, x)
 
        # Increment count by
        # minimum of B and y
        count += min(B, y)
 
        # Update B
        B -= min(B, y)
 
        # Return count
    return count
 
# Driver Code
if __name__ == "__main__":
 
    s = "*???*???*"
    A = 4
    B = 3
 
    print(maximumChar(s, A, B))
 
    # This code is contributed by rakeshsahni


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG {
 
    // Function to count the maximum number
    // of characters 'A' and 'B' that can be
    // placed in position of '?'
    public static int maximumChar(String s, int A, int B)
    {
       
        // Length of the string
        int len = s.Length;
 
        // Store the current count of '?'s
        int curr = 0;
 
        // Store the lengths of contiguous
        // segments of '?'s
        List<int> v = new List<int>();
 
        // Traversing the string
        for (int i = 0; i < len; i++)
        {
           
            // If character is '?'
            // increment curr by 1
            if (s[i] == '?') {
                curr++;
            }
 
            // If character is '*'
            else {
                // If curr is not equal to 0
                if (curr != 0) {
                    v.Add(curr);
 
                    // Re-initialise curr to 0
                    curr = 0;
                }
            }
        }
 
        // After traversing the string
        // if curr is not equal to 0
        if (curr != 0) {
            v.Add(curr);
        }
 
        // Variable for maximum count
        int count = 0;
 
        // Traversing the vector
        for (int i = 0; i < v.Count; i++)
        {
           
            // Variable to store half of
            // each elements in vector
            int x = v[i] / 2;
 
            // Variable to store half of
            // each elements with its remainder
            int y = v[i] / 2 + v[i] % 2;
 
            // If A is greater than B
            if (A > B)
            {
               
                // Swap both
                int temp = A;
                A = B;
                B = temp;
            }
 
            // Increment count by
            // minimum of A and x
            count += Math.Min(A, x);
 
            // Update A
            A -= Math.Min(A, x);
 
            // Increment count by
            // minimum of B and y
            count += Math.Min(B, y);
 
            // Update B
            B -= Math.Min(B, y);
        }
 
        // Return count
        return count;
    }
 
    // Driver Code
    public static void Main(String []args) {
        String s = "*???*???*";
        int A = 4, B = 3;
 
        Console.WriteLine(maximumChar(s, A, B));
    }
}
 
// This code is contributed by shikhasingrajput


Javascript




<script>
 
       // JavaScript Program to implement
       // the above approach
 
       // Function to count the maximum number
       // of characters 'A' and 'B' that can be
       // placed in position of '?'
       function maximumChar(s, A, B)
       {
        
           // Length of the string
           let len = s.length;
 
           // Store the current count of '?'s
           let curr = 0;
 
           // Store the lengths of contiguous
           // segments of '?'s
           let v = [];
 
           // Traversing the string
           for (let i = 0; i < len; i++)
           {
            
               // If character is '?'
               // increment curr by 1
               if (s[i] == '?') {
                   curr++;
               }
 
               // If character is '*'
               else
               {
                
                   // If curr is not equal to 0
                   if (curr != 0) {
                       v.push(curr);
 
                       // Re-initialise curr to 0
                       curr = 0;
                   }
               }
           }
 
           // After traversing the string
           // if curr is not equal to 0
           if (curr != 0) {
               v.push(curr);
           }
 
           // Variable for maximum count
           let count = 0;
 
           // Traversing the vector
           for (let i = 0; i < v.length; i++)
           {
            
               // Variable to store half of
               // each elements in vector
               let x = Math.floor(v[i] / 2);
 
               // Variable to store half of
               // each elements with its remainder
               let y = Math.floor(v[i] / 2) + v[i] % 2;
 
               // If A is greater than B
               if (A > B)
               {
                
                   // Swap both
                   let temp = A;
                   A = B;
                   B = temp;
               }
 
               // Increment count by
               // minimum of A and x
               count = count + Math.min(A, x);
 
               // Update A
               A = A - Math.min(A, x);
 
               // Increment count by
               // minimum of B and y
               count = count + Math.min(B, y);
 
               // Update B
               B = B - Math.min(B, y);
           }
 
           // Return count
           return count;
       }
 
       // Driver Code
       let s = "*???*???*";
       let A = 4, B = 3;
 
       document.write(maximumChar(s, A, B));
 
   // This code is contributed by Potta Lokesh
   </script>


Output

6

Time Complexity: O(N)
Auxiliary Space: O(N)