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++
#include <bits/stdc++.h>
using namespace std;
int maximumChar(string s, int A, int B)
{
int len = s.size();
int curr = 0;
vector< int > v;
for ( int i = 0; i < len; i++) {
if (s[i] == '?' ) {
curr++;
}
else {
if (curr != 0) {
v.push_back(curr);
curr = 0;
}
}
}
if (curr != 0) {
v.push_back(curr);
}
int count = 0;
for ( int i = 0; i < v.size(); i++) {
int x = v[i] / 2;
int y = v[i] / 2 + v[i] % 2;
if (A > B) {
swap(A, B);
}
count += min(A, x);
A -= min(A, x);
count += min(B, y);
B -= min(B, y);
}
return count;
}
int main()
{
string s = "*???*???*" ;
int A = 4, B = 3;
cout << maximumChar(s, A, B);
return 0;
}
|
Java
import java.util.ArrayList;
class GFG {
public static int maximumChar(String s, int A, int B)
{
int len = s.length();
int curr = 0 ;
ArrayList<Integer> v = new ArrayList<Integer>();
for ( int i = 0 ; i < len; i++) {
if (s.charAt(i) == '?' ) {
curr++;
}
else {
if (curr != 0 ) {
v.add(curr);
curr = 0 ;
}
}
}
if (curr != 0 ) {
v.add(curr);
}
int count = 0 ;
for ( int i = 0 ; i < v.size(); i++)
{
int x = v.get(i) / 2 ;
int y = v.get(i) / 2 + v.get(i) % 2 ;
if (A > B)
{
int temp = A;
A = B;
B = temp;
}
count += Math.min(A, x);
A -= Math.min(A, x);
count += Math.min(B, y);
B -= Math.min(B, y);
}
return count;
}
public static void main(String args[]) {
String s = "*???*???*" ;
int A = 4 , B = 3 ;
System.out.println(maximumChar(s, A, B));
}
}
|
Python3
import math
def maximumChar(s, A, B):
le = len (s)
curr = 0
v = []
for i in range ( 0 , le):
if (s[i] = = '?' ):
curr + = 1
else :
if (curr ! = 0 ):
v.append(curr)
curr = 0
if (curr ! = 0 ):
v.append(curr)
count = 0
for i in range ( 0 , len (v)):
x = v[i] / / 2
y = v[i] / / 2 + v[i] % 2
if (A > B):
temp = A
A = B
B = temp
count + = min (A, x)
A - = min (A, x)
count + = min (B, y)
B - = min (B, y)
return count
if __name__ = = "__main__" :
s = "*???*???*"
A = 4
B = 3
print (maximumChar(s, A, B))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int maximumChar(String s, int A, int B)
{
int len = s.Length;
int curr = 0;
List< int > v = new List< int >();
for ( int i = 0; i < len; i++)
{
if (s[i] == '?' ) {
curr++;
}
else {
if (curr != 0) {
v.Add(curr);
curr = 0;
}
}
}
if (curr != 0) {
v.Add(curr);
}
int count = 0;
for ( int i = 0; i < v.Count; i++)
{
int x = v[i] / 2;
int y = v[i] / 2 + v[i] % 2;
if (A > B)
{
int temp = A;
A = B;
B = temp;
}
count += Math.Min(A, x);
A -= Math.Min(A, x);
count += Math.Min(B, y);
B -= Math.Min(B, y);
}
return count;
}
public static void Main(String []args) {
String s = "*???*???*" ;
int A = 4, B = 3;
Console.WriteLine(maximumChar(s, A, B));
}
}
|
Javascript
<script>
function maximumChar(s, A, B)
{
let len = s.length;
let curr = 0;
let v = [];
for (let i = 0; i < len; i++)
{
if (s[i] == '?' ) {
curr++;
}
else
{
if (curr != 0) {
v.push(curr);
curr = 0;
}
}
}
if (curr != 0) {
v.push(curr);
}
let count = 0;
for (let i = 0; i < v.length; i++)
{
let x = Math.floor(v[i] / 2);
let y = Math.floor(v[i] / 2) + v[i] % 2;
if (A > B)
{
let temp = A;
A = B;
B = temp;
}
count = count + Math.min(A, x);
A = A - Math.min(A, x);
count = count + Math.min(B, y);
B = B - Math.min(B, y);
}
return count;
}
let s = "*???*???*" ;
let A = 4, B = 3;
document.write(maximumChar(s, A, B));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
07 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...