You can calculate the number of ? ( let’s call it n) that need to be replaced by + (let’s call it x) and the ones that need to be replaced by - .
If you cannot find non-negative integers for either then the answer will be 0.
Otherwise, the you can choose the number of spots where the + will go as nCx and the - will go in the rest of the spots.
Answer will be probablity of getting + multiplied by nCx
Thank you @preyam for replying,
Your Explaination was good but i didn’t understand it fully.
Can You please elaborate in more detail on the basis of the solution mentioned above,like how is he calculating ‘r’ in ‘nCr’.
Thanks in advance!
Look at the solutions for variable names.
The sum that you need in the ? places is (a-b) .
Let’s say that the number of + in the ? places in the final answer is x and the number of - is y.
x+y=n
x-y=a-b
on adding we will get x=(n+(a-b))/2 i.e. number of + that we need in the final answer which is also equal to r.
A trivial observation is that if the value of (p-s) for any two sequences is the same (where p and s denote the number of pluses and minuses respectively), they have the same final position. I’ll refer to this value as the balance.
Let’s find the balance for the original sequence and the interpreted sequence (ignoring the ? marks). Suppose the balance for the original sequence is 5, and the interpreted sequence is 3, and you have 6 ‘?’ marks in the interpreted sequence.
You now know that you need to replace the ‘?’ marks with a sequence whose balance, when added to 3, makes the entire balance 5. This is obviously 2.
We need to place pluses and minuses such that the number of pluses (let it be p) is 2 greater than number of minuses (let it be m). You also know that p+m = q, where q is the number of question marks (6 in this case).
Your two equations are p+m=q and p-m=2. Solve those, and you get p=4 and m=2.
You now need to count number of ways to arrange these pluses and minuses in the place of question marks. This count is q \choose p.
If number of question marks + the distance covered is odd, there is no way Dreamoon can reach b (because we’re left with an odd parity. We can only reach b - 1, or b + 1). Similarly, if the number of question marks is less than abs(a - b), Dreamoon cannot move to b, even if he chooses all + or all -.
N is the number of ?, so essentially the number of choices that are made. b is the coordinate where Dreamoon is supposed to reach. a is the coordinate where Dreamoon is currently standing.
Now Dreamoon has to move a total of abs(a - b) steps. So out of N?s, we choose exaxtly abs(a - b) and turn them to + (WLOG). Now out of the remaining abs(a - b)?s, we choose half of that and turn it to +, and the rest to -.
This means we are flipping a total of (N - abs(a - b)/2 + abs(a - b))?s. Which essentially simplifies to (N + abs(a - b))/2, which is his r.
@aneee004@preyam@shivensinha4, I literally want to thank you guys.I was Struggling with this doubt since evening.Now i understood how r is calculated with a much genuine explaination.
Yes this is my bitmasking part,and yes i am checking all 2^n subsets but here n is the number of question marks(’?’)
So in Worst Case if whole string string contains ‘?’ then it will be an O(n) Solution.