Adhoc Problem , but not intuitive

Hi I’m struggling on one of the famous problem asked for Microsoft Assessment round. My solution is not working. Please can someone help me for which test case it will Fail.

Problem - link
Problem Statement - pic

My solution -link

Whats wrong in my approach , looks easy and clear still not able to pass all cases

Help me folks

What is your problem CE or WA?


Find the rightmost A and count all B to its left, call that cntB.
Then find the leftmost B and count all A to its right, call that cntA.
ans = min(cntA, cntB)
Works in O(n)
I haven’t verified any corner cases though.

Thanks for reply.
This one is not working for all cases.

Case1: String only consists of A’s. Delete all B’s.
Case2: String only consists of B’s. Delete all A’s.
Case3: String consists of prefix of A’s of length x. Suffix of B’s n-x. Try for all x.

Answer must be min of 3 cases.

There was a similar problem like this one in the April Circuits on Hackerearth.
You can have a look at it.