Rules for good string:
-
If at index i, the char is ‘1’ and char at i-1 is ‘2’, then char at i-2 should be ‘2’.
-
If at index i, the char is ‘2’ and char at i-1 is ‘2’, then char at i-2 should be ‘1’.
Algorithm:
-
Traverse the two input strings and check for the above two conditions.
-
If the conditions doesn’t satisfy at index i, swap the chars of both strings at i.
-
Again if the conflict occurs at the same index, the ans is 0.
-
If the conditions are satisfied at all indexes, the answer is a power of 2.
Steps to calculate ans:
-
Count the no. of indexes at which the chars are same. Let this count be c.
-
There may be indexes at which the chars are not same. Count the pairs of such indexes. Let this count be p.
-
The ans is pow(2,c+p)
Code: https://www.codechef.com/viewsolution/11855552
Time: 0.03