CHEFTWOS - Adhoc Approach

Rules for good string:

  1. If at index i, the char is ‘1’ and char at i-1 is ‘2’, then char at i-2 should be ‘2’.

  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:

  1. Traverse the two input strings and check for the above two conditions.

  2. If the conditions doesn’t satisfy at index i, swap the chars of both strings at i.

  3. Again if the conflict occurs at the same index, the ans is 0.

  4. If the conditions are satisfied at all indexes, the answer is a power of 2.

Steps to calculate ans:

  1. Count the no. of indexes at which the chars are same. Let this count be c.

  2. There may be indexes at which the chars are not same. Count the pairs of such indexes. Let this count be p.

  3. The ans is pow(2,c+p)

Code: https://www.codechef.com/viewsolution/11855552
Time: 0.03

6 Likes

Can you explain why does it work?

Amazing explanation. I’ve looked over so many answers but haven’t found any matching this level of clarity. Good job !

simple and clear explanation :slight_smile:

i solved the same way 0.04 time …nothing special about this question…just do what problem said. make good all work is done here.calculation is easy after anspection of a few test cases i guess.