Approach

Can any one tell me the approach of the following question?

Stuart is playing a game of strings with his friend. His friend is new to the game, so he is telling him how to play the game. The game has 2 binary strings A=a1a2a3..an and B=b1b2b3..bn of equal length.

You need to transfer a to b, that is make them equal using the following operations in any order and as many times as you want:

Shift a by one character to the left. (if a = a1a2…an, replace a with a2a3a4..ana1).
Shift a by one character to the right.(if a = a1a2a3…an replace a with ana1a2a3…an-1).
Choose any i such that Bi = 1. Flip Ai (set Ai = 1 – Ai).
Print the smallest number of operations required to achieve this if possible or print -1 if it is not possible.

Constraints

1 <= |A| = |B| <= 2000
A and B consist of 0 and 1.

Test Case:
1010
1100
Output: 3
Test Case:
11010
10001
Output:4

Last edited on
you should focus on one of these at a time.
what have you done so far? Did you draw a picture or anything you can show us?
Topic archived. No new replies allowed.