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

• Last Updated : 07 Dec, 2021

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 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 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 Codeint main(){    string s = "*???*???*";    int A = 4, B = 3;     cout << maximumChar(s, A, B);     return 0;}

Java

 // Java program for the above approachimport 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 v = new ArrayList();         // 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 approachimport 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 Codeif __name__ == "__main__":     s = "*???*???*"    A = 4    B = 3     print(maximumChar(s, A, B))     # This code is contributed by rakeshsahni

C#

 // C# program for the above approachusing 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 v = new List();         // 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


Output
6

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

My Personal Notes arrow_drop_up