qyliang/CodeNanoFix huggingface dataset:
Buggy code:
# coding:utf-8

import sys
from collections import deque, Counter

INF = float('inf')
MOD = 10 ** 9 + 7
dy, dx = (0, -1, 0, 1), (1, 0, -1, 0)

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n = II()
    S = [SI() for _ in range(n)]

    cnt = {"B_": 0, "_A": 0, "B_A": 0}
    res = 0
    for s in S:
        for i in range(len(s) - 1):
            if s[i: i + 2] == 'AB':
                res += 1

        if s[0] == 'B' and s[-1] == 'A':
            cnt["B_A"] += 1
        elif s[0] == 'B':
            cnt["B_"] += 1
        elif s[-1] == 'A':
            cnt["_A"] += 1

    # print(res, cnt)
    while cnt["B_A"] > 0:
        if cnt["B_"] < cnt["_A"]:
            cnt["B_"] += 1
        else:
            cnt["_A"] += 1
        cnt["B_A"] -= 1

    # print(res, cnt)

    print(res + min(cnt["B_"], cnt["_A"]))
    

if __name__ == '__main__':
    main()

Fixed code:
# coding:utf-8

import sys
from collections import deque, Counter

INF = float('inf')
MOD = 10 ** 9 + 7
dy, dx = (0, -1, 0, 1), (1, 0, -1, 0)

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n = II()
    S = [SI() for _ in range(n)]

    cnt = {"B_": 0, "_A": 0, "B_A": 0}
    res = 0
    for s in S:
        for i in range(len(s) - 1):
            if s[i: i + 2] == 'AB':
                res += 1

        if s[0] == 'B' and s[-1] == 'A':
            cnt["B_A"] += 1
        elif s[0] == 'B':
            cnt["B_"] += 1
        elif s[-1] == 'A':
            cnt["_A"] += 1

    # print(res, cnt)
    res += min(cnt["B_"], cnt["_A"])
    if max(cnt["B_"], cnt["_A"]) > 0:
        res += cnt["B_A"]
    else:
        res += max(0, cnt["B_A"] - 1)

    print(res)


if __name__ == '__main__':
    main()
Buggy code:
n = int(input())
count = 0

a, b, ab = 0, 0, 0
for _ in range(n):
    s = input()
    if s[-1] == "A" and s[0] == "B":
        ab += 1
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1

    for i in range(len(s)-1):
        if s[i:i+2] == "AB":
            count += 1
            
count += min(a, b)
if b == ab:
    count -= 1

print(count)

Fixed code:
n = int(input())
count = 0

a, b, ab = 0, 0, 0
for _ in range(n):
    s = input()
    if s[-1] == "A" and s[0] == "B":
        ab += 1
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1


    for i in range(len(s)-1):
        if s[i:i+2] == "AB":
            count += 1

count += min(a, b)
if max(a, b) == ab and ab != 0:
    count -= 1

print(count)
Buggy code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 12 12:43:59 2019

@author: Yamazaki Kenichi
"""

N = int(input())
S = [input() for i in range(N)]

ans = 0
firstB, lastA, u = 0, 0, 0
for s in S:
    flg = False
    for c in s:
        if flg and c == "B":
            ans += 1
        flg = False
        if c == 'A':
            flg = True
    if s[0] == 'B':
        firstB += 1
    if s[-1] == 'A':
        lastA += 1
    if s[0] == 'B' and s[-1] == 'A':
        u += 1
ans += min(firstB,lastA,u-1)
print(ans)

Fixed code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 12 12:43:59 2019

@author: Yamazaki Kenichi
"""

N = int(input())
S = [input() for i in range(N)]

ans = 0
firstB, lastA, u = 0, 0, 0
for s in S:
    flg = False
    for c in s:
        if flg and c == "B":
            ans += 1
        flg = False
        if c == 'A':
            flg = True
    if s[0] == 'B':
        firstB += 1
    if s[-1] == 'A':
        lastA += 1
    if s[0] == 'B' and s[-1] == 'A':
        u += 1
ans += min(firstB,lastA)
ans -= 1 if u == firstB and u == lastA and u != 0 else 0
print(ans)
Buggy code:
n = int(input())
fully_ab = 0
last_a = 0
head_b = 0
head_last_both = 0

for i in range(n):
    s = input().rstrip()
    fully_ab +=s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        head_last_both += 1
    elif s[0] == "B":
        head_b += 1
    elif s[-1] == "A":
        last_a += 1
    
assert (last_a+head_b+head_last_both <= n)
bigger = max(last_a,head_b)
lower  = min(last_a,head_b)
amari = bigger - lower

connected = max(0,head_last_both - 1)

ans = fully_ab + lower + connected

if lower > 0 and head_last_both > 0:
    if lower + connected < n:
        ans += 1
print(ans)

Fixed code:
n = int(input())
fully_ab = 0
last_a = 0
head_b = 0
head_last_both = 0

for i in range(n):
    s = input().rstrip()
    fully_ab +=s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        head_last_both += 1
    elif s[0] == "B":
        head_b += 1
    elif s[-1] == "A":
        last_a += 1
    
assert (last_a+head_b+head_last_both <= n)
bigger = max(last_a,head_b)
lower  = min(last_a,head_b)
amari = bigger - lower

connected = max(0,head_last_both - 1)

ans1 = fully_ab
ans2 = lower + connected

if (amari>0 or lower > 0) and head_last_both > 0:
    ans2 += 1
print(ans1+ans2)
Buggy code:
N = int(input())
cnt, A, B = 0, 0, 0
for _ in range(N):
    S = input().rstrip()
    flg = False
    for s in S:
        if s=="A":
            flg=True
        elif flg==True and s=="B":
            cnt += 1
            flg = False
        else: flg = False
    if S[0]=="B": B+=1
    if S[-1]=="A": A+=1
if A==B: print(cnt+A-1)
else: print(cnt+min(A,B))

Fixed code:
N = int(input())
cnt, A, B, C = 0, 0, 0, 0
for _ in range(N):
    S = input().rstrip()
    flg = False
    for s in S:
        if s=="A":
            flg=True
        elif flg==True and s=="B":
            cnt += 1
            flg = False
        else: flg = False
    if S[0]=="B": B+=1
    if S[-1]=="A": A+=1
    if S[0]=="B" and S[-1]=="A": C+=1
if A==B==C and A!=0: print(cnt+A-1)
else: print(cnt+min(A,B))
Buggy code:
N = int(input())
s = [(input()) for _ in range(N)]

cntA = 0
cntB = 0
cntT = 0
ans = 0
for i in s:
    if i[0] == 'B' and i[-1] == 'A':
        cntT += 1
    else:
        if i[0] == 'B':
            cntB += 1
        if i[-1] == 'A':
            cntA += 1
    ans += i.count('AB')

if cntT > 1:
    ans += cntT - 1
    cntT = 1

if cntA > 0 and cntB > 0:
    ans += 2
    cntT -= 1
    cntA -= 1
    cntB -= 1

if cntT > 0:
    if cntA > 0 and cntA >= cntB:
        cntB += 1
    elif cntB > 0:
        cntA += 1

print(ans + min(cntA, cntB))

Fixed code:
N = int(input())
s = [(input()) for _ in range(N)]

cntA = 0
cntB = 0
cntT = 0
ans = 0
for i in s:
    if i[0] == 'B' and i[-1] == 'A':
        cntT += 1
    else:
        if i[0] == 'B':
            cntB += 1
        if i[-1] == 'A':
            cntA += 1
    ans += i.count('AB')

if cntT > 1:
    ans += cntT - 1
    cntT = 1

if cntA > 0 and cntB > 0 and cntT > 0:
    ans += 2
    cntT -= 1
    cntA -= 1
    cntB -= 1

if cntT > 0:
    if cntA > 0 and cntA >= cntB:
        cntB += 1
    elif cntB > 0:
        cntA += 1

print(ans + min(cntA, cntB))
Buggy code:
N=int(input())
L=[]
ans=0
A=0
B=0
AB=0
for i in range (N):
        L.append(input())
        ans+=L[i].count("AB")
        A+=L[i][-1].count("A")
        B+=L[i][0].count("B")
        if L[i][-1]=="A" and L[i][0]=="B":
                AB += 1
if A>=B:
        ans+=B
        if AB==B:
                ans+=-1
else:
        ans+=A
        if AB==A:
                ans+=-1
print(ans)

Fixed code:
N=int(input())
L=[]
ans=0
A=0
B=0
AB=0
for i in range (N):
        L.append(input())
        ans+=L[i].count("AB")
        A+=L[i][-1].count("A")
        B+=L[i][0].count("B")
        if L[i][-1]=="A" and L[i][0]=="B":
                AB += 1
if A>=B:
        ans+=B
        if AB==A and AB>=1:
                ans-=1
else:
        ans+=A
        if AB==B and AB>=1:
                ans-=1            
print(ans)
Buggy code:
N = int(input())
B_start_cnt = 0
A_end_cnt = 0
AB_cnt = 0
BA_cnt = 0
for i in range(N):
    s = input()
    if s[0] == "B" and s[-1] == "A":
        BA_cnt += 1
    if s[0] == "B" and s[-1] != "A":
        B_start_cnt += 1
    if s[-1] == "A" and s[0] == "B":
        A_end_cnt += 1
    AB_cnt += s.count('AB')
ans = AB_cnt

if BA_cnt == 0:
    ans += min(A_end_cnt,B_start_cnt)
elif max(A_end_cnt,B_start_cnt)>0:
    ans += BA_cnt+min(A_end_cnt,B_start_cnt)
else:
    ans += BA_cnt-1
print(ans)

Fixed code:
N = int(input())
B_start_cnt = 0
A_end_cnt = 0
AB_cnt = 0
BA_cnt = 0
for i in range(N):
    s = input()
    if s[0] == "B" and s[-1] == "A":
        BA_cnt += 1
    if s[0] == "B" and s[-1] != "A":
        B_start_cnt += 1
    if s[-1] == "A" and s[0] != "B":
        A_end_cnt += 1
    AB_cnt += s.count('AB')
ans = AB_cnt

if BA_cnt == 0:
    ans += min(A_end_cnt,B_start_cnt)
elif max(A_end_cnt,B_start_cnt)>0:
    ans += BA_cnt+min(A_end_cnt,B_start_cnt)
else:
    ans += BA_cnt-1
print(ans)
Buggy code:
n=int(input())
ba,a,b,ab=0,0,0,0
for i in range(n):
    s=input()
    if s[0]=='B' and s[-1]=='A':
        ba+=1
    elif s[0]=='B':
        b+=1
    elif s[-1]=='A':
        a+=1
    for i in range(len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ab+=1

print(min(a,b)+x+ab)


Fixed code:
n=int(input())
ba,a,b,ab=0,0,0,0
for i in range(n):
    s=input()
    if s[0]=='B' and s[-1]=='A':
        ba+=1
    elif s[0]=='B':
        b+=1
    elif s[-1]=='A':
        a+=1
    for i in range(len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ab+=1
if a==b and a==0 and ba!=0:
	ba-=1
print(min(a,b)+ba+ab)
Buggy code:
n = int(input())
S = [input() for i in range(n)]

ans = 0
start_b = 0
end_a = 0
ab = 0

for i in range(n):
    for j in range(len(S) - 1):
        if S[i][j] + S[i][j + 1] == 'AB':
            ans += 1

    if S[i][0] == 'B' and S[i][-1] == 'A':
        ab += 1
        continue 
    if S[i][0] == 'B':
        start_b += 1
    if S[i][-1] == 'A':
        end_a += 1
    

if ab > 0:
    ans += ab - 1
    if start_b > 0:
        ans += 1
        start_b -= 1
    if end_a > 0:
        ans += 1
        end_a -= 1
        
ans += min(start_b, end_a)

print(ans)

Fixed code:
n = int(input())
S = [input() for i in range(n)]

ans = 0
start_b = 0
end_a = 0
ab = 0

for i in range(n):
    for j in range(len(S[i]) - 1):
        if S[i][j:j+2] == 'AB':
            ans += 1

    if S[i][0] == 'B' and S[i][-1] == 'A':
        ab += 1
        continue 
    if S[i][0] == 'B':
        start_b += 1
    if S[i][-1] == 'A':
        end_a += 1
    

if ab > 0:
    ans += ab - 1
    if start_b > 0:
        ans += 1
        start_b -= 1
    if end_a > 0:
        ans += 1
        end_a -= 1
        
ans += min(start_b, end_a)

print(ans)
Buggy code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
import re
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    s = SR(n)
    a = 0
    b = 0
    ans = 0
    for si in s:
        if si[0] == "B":
            if a:
                a -= 1
                ans += 1
        if si[-1] == "A":
            if b:
                b -= 1
                ans += 1
        a += si[-1] == "A"
        b += si[0] == "B"
        ans += si.count("AB")
    print(ans)

    return


#main
if __name__ == '__main__':
    solve()


Fixed code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
import re
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    s = SR(n)
    a = 0
    b = 0
    ab = 0
    ans = 0
    for si in s:
        if si[0] == "B" and si[-1] == "A":
            ab += 1
        elif si[0] == "B":
            b += 1
        elif si[-1] == "A":
            a += 1
        ans += si.count("AB")
    if ab:
        ans += ab - 1
        ans += (a > 0) + (b > 0)
        a -= a > 0
        b -= b > 0
    ans += min(a, b)
    print(ans)

    return


#main
if __name__ == '__main__':
    solve()
Buggy code:
N = int(input())
a = 0
b = 0
c = 0
v = 0

for i in range(N):
    S = input()
    for j in range(len(S)-1):
        if S[j:j+2] == 'AB':
            c += 1
    if S[0] == 'B' and S[-1] == 'A':
        v += 1
        continue
    if S[0] == 'B':
        b += 1
    if S[-1] == 'A':
        a += 1
        
if v >= 1:
    if a < b:
        a += 1
    else:
        b += 1

print(c + v + min(a, b))


Fixed code:
N = int(input())
a = 0
b = 0
c = 0
v = 0

for i in range(N):
    S = input()
    for j in range(len(S)-1):
        if S[j:j+2] == 'AB':
            c += 1
    if S[0] == 'B' and S[-1] == 'A':
        v += 1
        continue
    if S[0] == 'B':
        b += 1
    if S[-1] == 'A':
        a += 1
'''
if v >= 1:
    if a < b:
        a += 1
    else:
        b += 1
'''
if a >= 1 or b >= 1:
    print(c + v + min(a, b))
else:
    print(c + max(0, v-1))
Buggy code:
#a,b,x=map(int,input().split())

n=int(input())
s=[input() for i in range(n)]

ans=0
ab=0
xa=0
bx=0
for i in range(n):
    ans+=s[i].count('AB')

    if s[i][-1]=='A' and s[i][0]=='B':
        ab+=1
    elif s[i][-1]=='A':
        xa+=1
    elif s[i][0]=='B':
        bx+=1

cnt=min(ab+xa,ab+bx) if (xa!=0 or bx!=0) else ab-1
print(ans+cnt if cnt>0 else ans)
print(ans,cnt)

Fixed code:
#a,b,x=map(int,input().split())

n=int(input())
s=[input() for i in range(n)]

ans=0
ab=0
xa=0
bx=0
for i in range(n):
    ans+=s[i].count('AB')

    if s[i][-1]=='A' and s[i][0]=='B':
        ab+=1
    elif s[i][-1]=='A':
        xa+=1
    elif s[i][0]=='B':
        bx+=1

cnt=min(ab+xa,ab+bx) if (xa!=0 or bx!=0) else ab-1
print(ans+cnt if cnt>0 else ans)
Buggy code:
#k,a,b=map(int,input().split())
n=int(input())
s=[input() for i in range(n)]
fin_a=0
ini_b=0
ab=0
for i in s:
    if i[0]=='B' and i[-1]=='A':
        ab+=1
    elif i[0]=='B':
        ini_b+=1
    elif i[-1]=='A':
        fin_a+=1

s='+'.join(s)
ans=s.count('AB')
print(ans+min(fin_a+ab,ini_b+ab) if not (fin_a==0 and ini_b==0) else ans+max(ab-1,0))
print(ini_b,fin_a,ab)

Fixed code:
#k,a,b=map(int,input().split())
n=int(input())
s=[input() for i in range(n)]
fin_a=0
ini_b=0
ab=0
for i in s:
    if i[0]=='B' and i[-1]=='A':
        ab+=1
    elif i[0]=='B':
        ini_b+=1
    elif i[-1]=='A':
        fin_a+=1

s='+'.join(s)
ans=s.count('AB')
print(ans+min(fin_a+ab,ini_b+ab) if not (fin_a==0 and ini_b==0) else ans+max(ab-1,0))
Buggy code:
#d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#import numpy as np
#n=int(input())
#li=input().split()
#import fractions
#lis=list(map(int,input().split()))

n=int(input())
S=[]
ans=0
firstB,lastA,bothAB=0,0,0

for i in range(n):
    S.append(input())
    flag=0
    for j in range(len(S[-1])-1):
        if S[-1][j:j+2]=="AB":
            ans+=1
    if S[-1][0]=="B":
        firstB+=1
        flag+=1
    if S[-1][-1]=="A":
        lastA+=1
        flag+=1
    if flag==2:
        bothAB+=1
        firstB-=1
        lastA-=1

if max(firstB,lastA)!=0:
    if firstB==lastA:
        ans+=(2*bothAB+firstB-1)
    else:
        ans+=min(firstB,lastA)+bothAB
else:
    ans+=(bothAB-1)

print(ans)


Fixed code:
#d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#import numpy as np
#n=int(input())
#li=input().split()
#import fractions
#lis=list(map(int,input().split()))

n=int(input())
S=[]
ans=0
firstB,lastA,bothAB=0,0,0

for i in range(n):
    S.append(input())
    flag=0
    for j in range(len(S[-1])-1):
        if S[-1][j:j+2]=="AB":
            ans+=1
    if S[-1][0]=="B":
        firstB+=1
        flag+=1
    if S[-1][-1]=="A":
        lastA+=1
        flag+=1
    if flag==2:
        bothAB+=1
        firstB-=1
        lastA-=1

if firstB==lastA:
    if firstB==0 and bothAB!=0:
        ans+=bothAB-1
    else:
        ans+=(bothAB+firstB)
else:
    ans+=min(firstB,lastA)+bothAB

print(ans)
Buggy code:
tBeA = 0
tBeNA = 0
tNBeA = 0
for i in range(N):
    flag = False
    if a[i][0] == "B":
        if a[i][-1] == "A":
            tBeA += 1
        else:
            tBeNA += 1
    else:
        if a[i][-1] == "A":
            tNBeA += 1

    for j in range(len(a[i])):
        # print(a[i][j])
        if a[i][j] == "B" and flag:
            cnt += 1
        flag = False

        if a[i][j] == "A":
            flag = True

print(
    cnt + min(tBeNA, tNBeA) if tBeA == 0 else cnt + min(tBeNA, tNBeA) + tBeA if tNBeA + tBeNA > 0 else cnt + min(tBeNA,
                                                                                                                 tNBeA) + tBeA - 1)


Fixed code:
N = int(input())
a = [str(input()) for i in range(N)]
cnt = 0
tBeA = 0
tBeNA = 0
tNBeA = 0
for i in range(N):
    flag = False
    if a[i][0] == "B":
        if a[i][-1] == "A":
            tBeA += 1
        else:
            tBeNA += 1
    else:
        if a[i][-1] == "A":
            tNBeA += 1

    for j in range(len(a[i])):
        # print(a[i][j])
        if a[i][j] == "B" and flag:
            cnt += 1
        flag = False

        if a[i][j] == "A":
            flag = True

print(
    cnt + min(tBeNA, tNBeA) if tBeA == 0 else cnt + min(tBeNA, tNBeA) + tBeA if tNBeA + tBeNA > 0 else cnt + min(tBeNA,
                                                                                                                 tNBeA) + tBeA - 1)
Buggy code:
N = int(input())

b = 0
a = 0
count = 0
check = -1
for i in range(N):
    s = input()
    count += len([1 for j in range(len(s)) if s[j:j + 2] == "AB"])
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[0] == "B" and s[-1] == "A":
        check += 1

if a == b and b == check:
    print(count+b-1)
else:
    print(count + min([a, b, check]))

Fixed code:
N = int(input())
 
a = 0
b = 0
c = 0
count = 0
for i in range(N):
    s = input()
    count += len([1 for j in range(len(s)) if s[j:j+2] == "AB"])
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[0] == "B" and s[-1] == "A":
        c += 1

if a == b and b == c and c != 0:
    print(count + c - 1)
else:
    print(count + min([a, b, N-1]))
Buggy code:
n = int(input())
s = [input() for i in range(n)]

cnt = 0
cntBA = 0
cntA = 0
cntB = 0
for i in s:
    for j in range(len(i) - 1):
        if i[j:j + 2] == "AB":
            cnt += 1
    #print(i)
    if i[0] == "B" and i[-1] == "A":
        cntBA += 1
    elif i[0] == "B":
        cntB += 1
    elif i[-1] == "A":
        #print(123)
        cntA += 1

#print(cnt, cntA, cntB, cntBA)
ans = cnt + max(0, cntBA - 1)
ans += min(cntA, 1)
cntA -= 1
ans += min(cntB, 1)
cntB -= 1
ans += max(min(cntA, cntB), 0)

print(ans)

Fixed code:
n = int(input())
s = [input() for i in range(n)]

cnt = 0
cntBA = 0
cntA = 0
cntB = 0
for i in s:
    for j in range(len(i) - 1):
        if i[j:j + 2] == "AB":
            cnt += 1
    #print(i)
    if i[0] == "B" and i[-1] == "A":
        cntBA += 1
    elif i[0] == "B":
        cntB += 1
    elif i[-1] == "A":
        #print(123)
        cntA += 1

#print(cnt, cntA, cntB, cntBA)
ans = cnt + max(0, cntBA - 1)
if cntBA > 0:
    ans += min(cntA, 1)
    cntA -= 1
    ans += min(cntB, 1)
    cntB -= 1
ans += max(min(cntA, cntB), 0)


print(ans)
Buggy code:
N = int(input())
S =[0]*N
for i in range(N):
    S[i] = str(input())

ans = int(0)
a=int(0)
b=int(0)
c=int(0)
R=int(0)

for i in range(N):
    AB = S[i].split("AB")
    R = len(AB)-1

    ans += R
    R = 0

for i in range(N):
    if S[i].endswith('A') : a +=1
    if S[i].startswith('B') : b +=1
    if S[i].endswith('A') and S[i].startswith('B'): c +=1

if c==0: R=0
elif c==a and c==b:
    R = min(a, b)-1
else : R=min(a, b)

ans += R

print(ans)

Fixed code:
N = int(input())
S =[0]*N
for i in range(N):
    S[i] = str(input())

ans = int(0)
a=int(0)
b=int(0)
c=int(0)
R=int(0)

for i in range(N):
    AB = S[i].split("AB")
    R = len(AB)-1

    ans += R
    R = 0

for i in range(N):
    if S[i].endswith('A') : a +=1
    if S[i].startswith('B') : b +=1
    if S[i].endswith('A') and S[i].startswith('B'): c +=1

if c==0 and c==a and c==b: R=0
elif c==a and c==b:
    R = min(a, b)-1
else : R=min(a, b)

ans += R

print(ans)
Buggy code:
def main():
	N = int(input())
	ans = 0
	a_end = 0
	b_begin = 0
	a_end_b_begin = 0
	for i in range(N):
		S = input()
		ans += S.count("AB")
		if S[0] == "B":
			if S[-1] == "A":
				a_end_b_begin += 1
				continue
			b_begin += 1
		if S[-1] == "A":
			a_end += 1
	if a_end_b_begin > 0:
		ans += a_end_b_begin-1
	if b_begin > a_end:
		ans += 1
		b_begin -= 1
		a_end += 1
	elif a_end > b_begin:
		ans += 1
		b_begin += 1
		a_end -= 1
	elif a_end == b_begin:
		ans += 2
		b_begin -= 1
		a_end -= 1
	ans += min(b_begin, a_end)
	print(ans)


if __name__ == '__main__':
	main()

Fixed code:
def main():
	N = int(input())
	ans = 0
	a_end = 0
	b_begin = 0
	a_end_b_begin = 0
	for i in range(N):
		S = input()
		ans += S.count("AB")
		if S[0] == "B":
			if S[-1] == "A":
				a_end_b_begin += 1
				continue
			b_begin += 1
		if S[-1] == "A":
			a_end += 1
	if a_end_b_begin > 0:
		ans += a_end_b_begin if a_end+b_begin>0 else a_end_b_begin-1
	ans += min(a_end, b_begin)
	print(ans)


if __name__ == '__main__':
	main()
Buggy code:
import re

N = int(input())

startB = 0
stopA = 0
BtoA = 0
AB = 0

for i in range(N):
    s = input()
    if s.startswith("B") and s.endswith("A"):
        BtoA += 1
    elif s.startswith("B"):
        startB += 1
    elif s.endswith("A"):
        stopA += 1
    
    AB += len(re.findall("AB", s))

if abs(startB - stopA) > 0:
    ans = AB + min(startB, stopA) + BtoA
else:
    ans = AB + min(startB, stopA) + BtoA - 1
print(ans)

Fixed code:
import re

N = int(input())

startB = 0
stopA = 0
BtoA = 0
AB = 0

for i in range(N):
    s = input()
    if s.startswith("B") and s.endswith("A"):
        BtoA += 1
    elif s.startswith("B"):
        startB += 1
    elif s.endswith("A"):
        stopA += 1
    
    AB += len(re.findall("AB", s))


if BtoA == 0:
    ans = AB + min(startB, stopA)
elif startB + stopA > 0:
    ans = AB + min(startB, stopA) + BtoA
else:
    ans = AB + min(startB, stopA) + BtoA - 1
print(ans)