My code giving wrong answer , please help

according to your logic inside the for loop at line 19, you are just checking for matching just once
for eg let say at present, q1 is 1 4 2 3 and q2 is 2 3 1 4, here you have to do 2 times operation on queue 1 because 2 is present at third place in q1 and at first place in q2.
So just have to put your line 29 and 30 inside the else statement.

One more thing, that your for loop is running only n time but there will be more than n iterations if q1 and q2 are different initially.
So one easy thing you can do for this is use while(q2.empty()==false) i.e. continue until the ideal string is not empty.
And you should only pop from q2 if the the element in front of both the queues is same.

Feel free to ask any furthur doubts.
Please mark it as resolved if you got it.

Thank you for the idea, but one more way is to just replace the if statement on line 23 with the while and the code worked. Thanks for your inputs though :slight_smile: