Online Python Compiler

class Solution: def solve(self, s): a_right = s.count("A") b_left = 0 ans = a_right for i, c in enumerate(s): if c == "A": a_right -= 1 else: b_left += 1 ans = min(ans, a_right + b_left) return ans ob = Solution() S = "AABAABB" print(ob.solve(S))