// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
//// Recursive function to minimum steps
// required after combining two strings
int minSteps(string& S, int i, int count1, int count2,
int first, int second, int n)
{
// If current pointer reaches the end
if (i == n) {
return 0;
}
// Condition to conclude that one string does
// more ones than the other irrespective of what
// number is chosen for the remaining indexes
if (count1 > (second + count2)
|| count2 > (first + count1)) {
return 0;
}
int c1 = 0, c2 = 0;
// If i is even, then choosing character for S1
if (i % 2 == 0) {
if (S[i] == '?') {
return min(
1
+ minSteps(
S, i + 1,
count1 + 1, count2,
first - 1, second, n),
1
+ minSteps(
S, i + 1, count1, count2,
first - 1, second, n));
}
else if (S[i] == '1') {
c1 = 1
+ minSteps(
S, i + 1,
count1 + 1, count2,
first - 1, second, n);
return c1;
}
else {
c2 = 1
+ minSteps(
S, i + 1,
count1, count2,
first - 1, second, n);
return c2;
}
}
// If i is odd
else {
if (S[i] == '?') {
return min(
1
+ minSteps(
S, i + 1,
count1, count2 + 1,
first, second - 1, n),
1
+ minSteps(
S, i + 1,
count1, count2,
first, second - 1, n));
}
else if (S[i] == '1') {
c1 = 1
+ minSteps(
S, i + 1,
count1, count2 + 1,
first, second - 1, n);
return c1;
}
else {
c2 = 1
+ minSteps(
S, i + 1, count1,
count2, first,
second - 1, n);
return c2;
}
}
}
// Driver Code
int main()
{
string s = "?10?0?";
int N = s.size();
cout << minSteps(s, 0, 0, 0,
N / 2, N / 2, N);
return 0;
}