qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import re
aflag = 0
bflag = 0
cnt = 0
cntna = 0
N = int(input())
for i in range(N):
    s = input()
    if s[-1:] == "A":
        aflag += 1
    if s[:1] == "B":
        bflag += 1
    if re.search("AB", s):
        cnt += 1
    if re.search("AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB.*AB", s):
        cnt += 1
    if re.search("ABABABABAB", s):
        cnt += 1
    if s[-1:]=="A" and s[:1]=="B":
        cntna += 1

cnt += min(aflag, bflag)
if min(aflag,bflag) == cntna:
    cnt -= 1
print(cnt)

Fixed code:
import re
aflag = 0
bflag = 0
cnt = 0
cntna = 0

N = int(input())
for i in range(N):
    s = input()
    if s[-1:] == "A":
        aflag += 1
    if s[:1] == "B":
        bflag += 1
    if re.search("AB", s):
        cnt += 1
    if re.search("AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB.*AB", s):
        cnt += 1
    if re.search("ABABABABAB", s):
        cnt += 1
    if s[-1:]=="A" and s[:1]=="B":
        cntna +=1

cnt += min(aflag, bflag)
if min(aflag,bflag) == N:
    cnt -= 1
if cntna == min(aflag, bflag) and aflag == bflag and aflag != 0 and bflag != 0:
    cnt -= 1
if aflag == bflag == N:
    cnt += 1
print(cnt)
Buggy code:
def c(strs):
    count = 0
    count_a = 0
    count_b = 0
    flag = 1
    for s in strs:
        count += s.count('AB')
        if s[0]=='B' and s[-1]=='A':
            count_a += 1
            count_b += 1
        elif s[0]=='B':
            count_b += 1
            flag = 0
        elif s[-1]=='A':
            count_a += 1
            flag = 0
    print(count,count_a,count_b)
    if count_a:
        return count + min(count_a,count_b) - flag
    return count


if __name__=='__main__':
    n = int(input())
    s = []
    for _ in range(n):
        s.append(input())
    print(c(s))



Fixed code:
def c(strs):
    count = 0
    count_a = 0
    count_b = 0
    flag = 1
    for s in strs:
        count += s.count('AB')
        if s[0]=='B' and s[-1]=='A':
            count_a += 1
            count_b += 1
        elif s[0]=='B':
            count_b += 1
            flag = 0
        elif s[-1]=='A':
            count_a += 1
            flag = 0
    if count_a:
        return count + min(count_a,count_b) - flag
    return count


if __name__=='__main__':
    n = int(input())
    s = []
    for _ in range(n):
        s.append(input())
    print(c(s))
Buggy code:
N=int(input())
s=[list(input()) for _ in range(N)]

ct_a=0
ct_b=0
ct_ab=0
ct_ba=0

for i in range(N):
    if s[i][0]=='B' and s[i][1]=='A' and len(s[i])==2:
        ct_ba+=1
    elif s[i][0]=='B':
        ct_b+=1
    elif s[i][-1]=='A':
        ct_a+=1
    for j in range(len(s[i])-1):
        if s[i][j]=='A' and s[i][j+1]=='B':
            ct_ab+=1
    
print(ct_ab+min(ct_a,ct_b)+ct_ba-1)

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

ct_a=0
ct_b=0
ct_ab=0
ct_ba=0

for i in range(N):
    if s[i][0]=='B' and s[i][-1]=='A':
        ct_ba+=1
    elif s[i][0]=='B':
        ct_b+=1
    elif s[i][-1]=='A':
        ct_a+=1
    for j in range(len(s[i])-1):
        if s[i][j]=='A' and s[i][j+1]=='B':
            ct_ab+=1

if ct_ba==0:
    print(ct_ab+min(ct_a, ct_b))
elif ct_a+ct_b>0:
    print(ct_ab+ct_ba+min(ct_a,ct_b))
else:
    print(ct_ab+ct_ba-1)
Buggy code:
n = int(input())
s = [list(input()) for i in range(n)]
a,b,c = 0,0,0
t = 0
for i in range(n):
        for j in range(len(s[i])-1):
                if s[i][j] == "A" and s[i][j+1] == "B":
                        c += 1
        if s[i][0] == "B":
                b += 1
        if s[i][-1] == "A":
                a += 1
        if s[i][0] == "B" and s[i][-1] == "A":
                t += 1

if a > b:
        d = b
else:
        d = a

if t == d:
        d = d - 1

print(d + c)

Fixed code:
n = int(input())
s = [list(input()) for i in range(n)]
a,b,c = 0,0,0
t = 0
for i in range(n):
        for j in range(len(s[i])-1):
                if s[i][j] =="A" and s[i][j+1] == "B":
                        c += 1
                else:
                        continue
        if s[i][0] == "B":
                b += 1
        if s[i][-1] == "A":
                a += 1
        if s[i][0] == "B" and s[i][-1] == "A":
                t += 1
 
if a > b:
        d = b
else:
        d = a

if  t == b and a == t:
        d -= 1
if  t == 0 and a == 0 and b ==0:
        d += 1

print(d + c)
Buggy code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = 0
b = 0
ans = 0
same = 0
for i in s:
    flg = False
    if i[0]=='B':
        flg = True
        b += 1
    if i[-1]=='A':
        a += 1
        if flg:
            same += 1
    for j in range(len(i)-1):
        if i[j:j+2]=='AB':
            ans += 1
ans += min(n-1, a, b)
if min(a, b) == same:
    ans -= 1
print(ans)

Fixed code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = 0
b = 0
ans = 0
same = 0
for i in s:
    flg = False
    if i[0]=='B':
        flg = True
        b += 1
    if i[-1]=='A':
        a += 1
        if flg:
            same += 1
    for j in range(len(i)-1):
        if i[j:j+2]=='AB':
            ans += 1
ans += min(a, b)
if a == b == same > 0:
    ans -= 1
print(ans)
Buggy code:
n = int(input())
s= [input() for i in range(n)]
ans1=0
firstB=0
lastA=0
same=0
for i in s:
    ans1+=i.count("AB")
    if i[0]=="B":
        firstB+=1
    if i[-1]=="A":
        lastA+=1
    if i[0]=="B" and i[-1]=="A":
        same+=1
if same==min(firstB,lastA):
    print(ans1+same-1)
else:
    print(ans1+min(firstB,lastA))

Fixed code:
n = int(input())
s= [input() for i in range(n)]
ans1=0
firstB=0
lastA=0
m=n
for i in s:
    ans1+=i.count("AB")
    if i[0]=="B":
        firstB+=1
    if i[-1]=="A":
        lastA+=1
    if i[0]!="B" and i[-1]!="A":
        m-=1
if min(firstB,lastA)==m:
    if m==0:
        print(ans1)
    else:
        print(ans1+m-1)
else:
    print(ans1+min(firstB,lastA))
Buggy code:
n = int(input())
s_list = [input() for x in range(n)]

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for x in s_list:
    cnt += x.count("AB")
    if x[-1] == "A":
        a_cnt += 1
    if x[0] == "B":
        b_cnt += 1
    if (x[-1] == "A")&(x[0] == "B"):
        ab_cnt += 1
print(cnt+min(a_cnt,b_cnt)-(ab_cnt-1))

Fixed code:
n = int(input())
s_list = [input() for x in range(n)]

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for x in s_list:
    cnt += x.count("AB")
    if (x[-1] == "A")&(x[0] != "B"):
        a_cnt += 1
    if (x[0] == "B")&(x[-1] != "A"):
        b_cnt += 1
    if (x[-1] == "A")&(x[0] == "B"):
        ab_cnt += 1
if ab_cnt == 0:
    print(cnt+min(a_cnt,b_cnt))
elif (a_cnt+b_cnt>0)&(ab_cnt != 0):
    print(cnt+ab_cnt+min(a_cnt,b_cnt))
elif (a_cnt+b_cnt==0):
    print(cnt+ab_cnt-1)
Buggy code:
N = int(input())
S = [input() for _ in range(N)]

cnt = 0
end_a = 0
sta_b = 0
a_b = 0
for s in S:
    cnt += s.count('AB')
    if s[-1]=='A' and s[0]=='B':
        a_b += 1
    elif s[-1]=='A':
        end_a += 1
    elif s[0]=='B':
        sta_b += 1

a_b = [a_b, a_b]
if a_b[0]>=end_a:
    cnt += end_a
    a_b[0] -= end_a
    end_a = 0
else:
    cnt += a_b
    a_b[0] = 0
    end_a -= a_b

if a_b[1]>=sta_b:
    cnt += sta_b
    a_b[1] -= sta_b
    sta_b = 0
else:
    cnt += a_b
    a_b[1] = 0
    sta_b -= a_b
cnt += min(end_a, sta_b)
cnt += min(a_b)
print(cnt)

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

cnt = 0
end_a = 0
sta_b = 0
a_b = 0
for s in S:
    cnt += s.count('AB')
    if s[-1]=='A' and s[0]=='B':
        a_b += 1
    elif s[-1]=='A':
        end_a += 1
    elif s[0]=='B':
        sta_b += 1
if a_b>=1:
    cnt += a_b-1
    if end_a>=1:
        end_a -= 1
        cnt += 1
    if sta_b>=1:
        sta_b -=1
        cnt+=1
cnt += min(end_a, sta_b)

print(cnt)
Buggy code:
n=int(input())
string=[input() for i in range(n)]

base_ab=0
last_a=0
first_b=0
all_ba=0
for s in string:
    base_ab+=s.count("AB")
    if s[-1]=="A":
        last_a+=1
    if s[0]=="B":
        first_b+=1
    if s[0]=="B" and s[-1]=="A":
        all_ba+=1
if min(last_a,first_b) == all_ba:
    print(base_ab+min(last_a,first_b)-1)
else:    
    print(base_ab+min(last_a,first_b))


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

base_ab=0
last_a=0
first_b=0
all_ba=0
for s in string:
    base_ab+=s.count("AB")
    if s[-1]=="A":
        last_a+=1
    if s[0]=="B":
        first_b+=1
    if s[0]=="B" and s[-1]=="A":
        all_ba+=1
if last_a-all_ba==0 and first_b-all_ba==0 and all_ba>0:
    print(base_ab+min(last_a,first_b)-1)
else:    
    print(base_ab+min(last_a,first_b))
Buggy code:
n= int(input())
a = 0
b = 0
ab = 0
for i in range(n):
    s = input()
    for j in range(len(s)-1):
        if s[j] == "A" and s[j+1] == "B":
            ab+=1
    if s[len(s)-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
if a == b and a == 0:
    print(ab)
elif a != b:
    print(ab + min(a,b))
elif a == b:
    print(ab + a -1)

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


N=int(input())
L=[] #(lastA, firstB, have AB)
ans=0
lastA=0
firB=0
AB=0
for i in range(N):
    s=input()
    c=s.count("AB")
    #print(c)
    a=s[-1]=="A"
    b=s[0]=="B"
    d=(a==b==1)
    if d:
        a=0
        b=0
    ans+=c
    lastA+=a
    firB+=b
    AB+=d
print(ans)
if lastA and firB:
    ans+=2+AB-1
    lastA-=1
    firB-=1
    ans+=min(lastA, firB)
elif lastA or firB:
    ans+=AB
else:
    ans+=max(AB-1,0)
print(ans, lastA, firB, AB)

Fixed code:


N=int(input())
L=[] #(lastA, firstB, have AB)
ans=0
lastA=0
firB=0
AB=0
for i in range(N):
    s=input()
    c=s.count("AB")
    #print(c)
    a=s[-1]=="A"
    b=s[0]=="B"
    d=(a==b==1)
    if d:
        a=0
        b=0
    ans+=c
    lastA+=a
    firB+=b
    AB+=d

if lastA and firB:
    ans+=2+AB-1
    lastA-=1
    firB-=1
    ans+=min(lastA, firB)
elif lastA or firB:
    ans+=AB
else:
    ans+=max(AB-1,0)
print(ans)
Buggy code:
n=int(input())
S=[input() for _ in range(n)]

ans,acnt,bcnt,abcnt=0,0,0,0
for s in S:
  ans+=s.count('AB')
  if s[-1]=='A': acnt+=1
  if s[0]=='B': bcnt+=1
  if s[-1]=='A' and s[0]=='B': abcnt+=1
print(ans+min(acnt,bcnt,abcnt))

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

ans,acnt,bcnt,abcnt=0,0,0,0
for s in S:
  ans+=s.count('AB')
  if s[-1]=='A' and s[0]=='B': abcnt+=1
  elif s[-1]=='A': acnt+=1
  elif s[0]=='B': bcnt+=1
if abcnt:
  ans+=abcnt-1
  if acnt:
    ans+=1
    acnt-=1
  if bcnt:
    ans+=1
    bcnt-=1
ans+=min(acnt,bcnt)
print(ans)
Buggy code:
N = int(input())
listS = ["" for _ in range(N)]
cntAB = 0
for i in range(N):
    listS[i] = input()
    for j in range(len(listS[i])-1):
        if listS[i][j] == "A" and listS[i][j+1] == "B":
            cntAB += 1
cntA = 0
cntB = 0
BcntA = 0
for s in listS:
    if s[0] == "B":
        cntB += 1
    if s[-1] == "A":
        cntA += 1
    if s[0] == "B" and s[-1] == "A":
        BcntA += 1
connect = 0
if min(cntA,cntB) == BcntA:
    connect = BcntA-1
else:
    connect = min(cntA,cntB)
print(connect+cntAB)


Fixed code:
N = int(input())
listS = ["" for _ in range(N)]
cntAB = 0
for i in range(N):
    listS[i] = input()
    for j in range(len(listS[i])-1):
        if listS[i][j] == "A" and listS[i][j+1] == "B":
            cntAB += 1
cntA = 0
cntB = 0
BcntA = 0
for s in listS:
    if s[0] == "B" and s[-1] != "A":
        cntB += 1
    if s[0] != "B" and s[-1] == "A":
        cntA += 1
    if s[0] == "B" and s[-1] == "A":
        BcntA += 1
connect = 0
if cntA + cntB == 0 and BcntA > 0:
    connect = BcntA - 1
else:
    connect = min(cntA,cntB) + BcntA
print(connect+cntAB)
Buggy code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ in range(n):
	s = input()
	for i in range(len(s)-1):
		if s[i] == "A" and s[i+1] == "B":
			ans += 1
	if s[0] == "B":
		if s[-1] == "A":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1
#print(xa, bx, bxa)
ans += bxa-1
if bxa > 0:
	if xa > 0:
		xa -= 1
		ans += 1
	if bx > 0:
		bx -= 1
		ans += 1
ans += min(xa, bx)
print(ans)

Fixed code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ in range(n):
	s = input()
	for i in range(len(s)-1):
		if s[i] == "A" and s[i+1] == "B":
			ans += 1
	if s[0] == "B":
		if s[-1] == "A":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1

if xa == bx:
	if xa == 0:
		print(ans+max(0, bxa-1))
	else:
		print(ans+xa+bxa)
else:
	print(ans+bxa+min(xa, bx))
Buggy code:
n=int(input())
s=[0]*n
ct=0
a=0
b=0
c=0
for i in range(n):
    s[i]=input()
for i in range(n):
    for j in range(len(s[i])):
        t=s[i]
        ts=list(t)
        ts.append(0)
        if ts[j]=='A' and ts[j+1]=='B':
            ct+=1
    if ts[0]=='B' and ts[len(s[i])-1]!='A':
        a+=1
    if ts[len(s[i])-1]=='A' and ts[0]!='B':
        b+=1
    if ts[0]=='B' and ts[len(s[i])-1]=='A':
        c+=1
if min(a,b)>0:
    print(ct+min(a,b)+c)
elif min(a,b)==0:
    print(ct+c-1)

Fixed code:
n=int(input())
s=[0]*n
ct=0
a=0
b=0
c=0
for i in range(n):
    s[i]=input()
for i in range(n):
    for j in range(len(s[i])):
        t=s[i]
        ts=list(t)
        ts.append(0)
        if ts[j]=='A' and ts[j+1]=='B':
            ct+=1
    if ts[0]=='B' and ts[len(s[i])-1]!='A':
        a+=1
    if ts[len(s[i])-1]=='A' and ts[0]!='B':
        b+=1
    if ts[0]=='B' and ts[len(s[i])-1]=='A':
        c+=1
if max(a,b)>0 or c==0:
    print(ct+min(a,b)+c)
elif max(a,b)==0:
    print(ct+c-1)
Buggy code:
n = int(input())
s = [input() for u in range(n)]
a = sum([u.count("AB") for u in s])
b = len([1 for u in s if u.endswith("A")])
c = len([1 for u in s if u.startswith("B")])
d = len([1 for u in s if u.endswith("A") and u.startswith("B")])
if d==1:
  print(a)
elif (b==c and c==d):
  print(a+b-1)
else:
  print(a+min(b,c))

Fixed code:
n = int(input())
s = [input() for u in range(n)]
a = sum([u.count("AB") for u in s])
b = len([1 for u in s if u.endswith("A")])
c = len([1 for u in s if u.startswith("B")])
d = len([1 for u in s if u.endswith("A") and u.startswith("B")])
if (b>0 and b==c and c==d):
  print(a+b-1)
else:
  print(a+min(b,c))
Buggy code:
import re

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

num_end_with_A = 0
num_start_with_B = 0
num_both = 0
num_inner_AB = 0

for ss in s:
    res = re.findall('AB', ss)
    if res:
        num_inner_AB += len(res)
    if ss[0] == 'B' and ss[-1] == 'A':
        num_both += 1
    elif ss[0] == 'B':
        num_start_with_B += 1
    elif ss[-1] == 'A':
        num_end_with_A += 1

if num_end_with_A == num_start_with_B:
    print(num_inner_AB + num_both - 1)
else:
    print(num_inner_AB + min(num_end_with_A, num_start_with_B) + num_both)


Fixed code:
import re

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

num_end_with_A = 0
num_start_with_B = 0
num_both = 0
num_inner_AB = 0

for ss in s:
    res = re.findall('AB', ss)
    if res:
        num_inner_AB += len(res)
    if ss[0] == 'B' and ss[-1] == 'A':
        num_both += 1
    elif ss[0] == 'B':
        num_start_with_B += 1
    elif ss[-1] == 'A':
        num_end_with_A += 1

if num_end_with_A == 0 and num_start_with_B == 0:
    print(num_inner_AB + max(num_both - 1, 0))
else:
    print(num_inner_AB + min(num_end_with_A, num_start_with_B) + num_both)
Buggy code:
n=int(input())
a,b,c=0,0,0
count=0
for i in range(n):
  s=input()
  count+=s.count('AB')
  if s[0]=='B' and s[-1]=='A':
    a+r=1
  elif s[0]=='B':
    b+=1
  elif s[-1]=='A':
    c+=1
if b==0 and c==0:
  print(count+a-1)
else:
  print(count+a+min(b,c))

Fixed code:
n=int(input())
a,b,c=0,0,0
count=0
for i in range(n):
  s=input()
  count+=s.count('AB')
  if s[0]=='B' and s[-1]=='A':
    a+=1
  elif s[0]=='B':
    b+=1
  elif s[-1]=='A':
    c+=1
if b==0 and c==0:
  print(count+max(0,a-1))
else:
  print(count+a+min(b,c))
Buggy code:
N = int(input())
S = []
for i in range(N):
    s = str(input())
    S.append(s)
    
count_AB = 0
for s in S:
    count_AB += s.count("AB")
# print(count_AB)

count_BA = 0
count_A = 0
count_B = 0
for s in S:
    if s[0] == 'B' and s[-1] == 'A':
        count_BA += 1
    if s[0] != 'B' and s[-1] == 'A':
        count_A += 1
    if s[0] == 'B' and s[-1] != 'A':
        count_B += 1
# print(count_BA, count_A, count_B)


if count_BA >= 1:
    count_AB += count_BA -1
if count_A >= 1:
    count_A -= 1
    count_AB += 1
if count_B >= 1:
    count_B -= 1
    count_AB += 1
count_AB += min(count_A, count_B)
print(count_AB)

Fixed code:
N = int(input())
S = []
for i in range(N):
    s = str(input())
    S.append(s)
    
count_AB = 0
for s in S:
    count_AB += s.count("AB")
# print(count_AB)

count_BA = 0
count_A = 0
count_B = 0
for s in S:
    if s[0] == 'B' and s[-1] == 'A':
        count_BA += 1
    if s[0] != 'B' and s[-1] == 'A':
        count_A += 1
    if s[0] == 'B' and s[-1] != 'A':
        count_B += 1
# print(count_BA, count_A, count_B)

if count_BA >= 1:
    count_AB += count_BA -1
if count_BA >= 1:
    if count_A >= 1:
        count_A -= 1
        count_AB += 1
    if count_B >= 1:
        count_B -= 1
        count_AB += 1
count_AB += min(count_A, count_B)
print(count_AB)
Buggy code:
N = int(input())
S = [input() for _ in range(N)]

top = [0 for _ in range(N)]
last = [0 for _ in range(N)]

res = 0
n = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            res += 1
            
    if s[0] == 'B':
        top[n] = 1
        
    if s[-1] == 'A':
        last[n] = 1
        
    n += 1
 
d = 0
t = 0
l = 0
for i in range(N):
    if top[i] == 1 and last[i] == 1:
        d += 1
    elif top[i] == 1:
        t += 1
    elif last[i] == 1:
        l += 1
        
res += d

res += min(t, l)

if d > 0 and max(t, l) - min(t, l) > 0:
    res += 1
    
    
print(res)

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

top = [0 for _ in range(N)]
last = [0 for _ in range(N)]

res = 0
n = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            res += 1
            
    if s[0] == 'B':
        top[n] = 1
        
    if s[-1] == 'A':
        last[n] = 1
        
    n += 1
 
d = 0
t = 0
l = 0
for i in range(N):
    if top[i] == 1 and last[i] == 1:
        d += 1
    elif top[i] == 1:
        t += 1
    elif last[i] == 1:
        l += 1
     

if d >= 1:
    res += d - 1



if d > 0 and min(t, l) >= 1:
    res += min(t-1, l-1)
    res += 2
    
elif d > 0 and min(t, l) < 1 and max(t, l) >= 1:
    res += 1
    
else:
    res += min(t, l)
    
    
print(res)
Buggy code:
def countAB(S):
  ct=0
  flg = True
  pos=0
  while flg:
    idx = S.find('AB',pos)
    if idx!=-1:
      ct+=1
    else:
      flg = False
    pos = idx + 2
  return ct

N = int(input())

num_AB = 0
num_A = 0
for n in range(N):
  S = input()
  num_AB += countAB(S)
  if S[len(S)-1]=='A':
    num_A += 1
  if S[0]=='B':
    num_B += 1

num_AB_cmb = min(num_A,num_B)
if num_A == N and num_B == N:
  num_AB_cmb = N-1

print("{0}".format(num_AB + num_AB_cmb))
  
  
  

Fixed code:
def countAB(S):
  ct=0
  flg = True
  pos=0
  while flg:
    idx = S.find('AB',pos)
    if idx!=-1:
      ct+=1
    else:
      flg = False
    pos = idx + 2
  return ct
 
N = int(input())
 
num_AB = 0
num_A = 0
num_B = 0
num_BA = 0
for n in range(N):
  S = input()
  num_AB += countAB(S)
  if S[len(S)-1]=='A':
    num_A += 1
  if S[0]=='B':
    num_B += 1
  if S[len(S)-1]=='A' and S[0]=='B': 
    num_BA += 1
    
num_AB_cmb = min(num_A,num_B)
if num_A == num_BA and num_B == num_BA and num_BA!=0:
  num_AB_cmb = num_BA-1
 
print("{0}".format(num_AB + num_AB_cmb))
Buggy code:
N=int(input())
XA=0
BA=0
BX=0
internalAB=0
for t in range(N):
    i=input()
    headUsed=False
    footUsed=False
    for x,j in enumerate(i):
        if x==len(i)-1:break
        if j=="A" and i[x+1]=="B":
            internalAB+=1
            if x==0:headUsed=True
            if x==len(i)-2:footUsed=True
    if i[0]=="B" and not headUsed:
        if i[-1]=="A" and not footUsed:
            BA+=1
        else:BX+=1
    elif i[-1]=="A" and not footUsed:
        XA+=1
ans=internalAB+BA+min(XA,BX)
if XA=BX=0:ans-=1
print(ans)


Fixed code:
N=int(input())
XA=0
BA=0
BX=0
internalAB=0
for t in range(N):
    i=input()
    headUsed=False
    footUsed=False
    for x,j in enumerate(i):
        if x==len(i)-1:break
        if j=="A" and i[x+1]=="B":
            internalAB+=1
            if x==0:headUsed=True
            if x==len(i)-2:footUsed=True
    if i[0]=="B" and not headUsed:
        if i[-1]=="A" and not footUsed:
            BA+=1
        else:BX+=1
    elif i[-1]=="A" and not footUsed:
        XA+=1
if XA or BX:
    externalAB=max(0,BA+min(XA,BX))
else:externalAB=max(0,BA-1)
ans=internalAB+externalAB
print(ans)
Buggy code:
n=int(input())
a=0
b=0
ab=0
num=0
for i in range(n):
    s=input()
    if s[0]=="B" and s[-1]=="A":
        ab+=1
    elif s[0]=="B":
        b+=1
    elif s[-1]=="A":
        a+=1
    for j in range(len(s)-1):
        if s[i]=="A" and s[i+1]=="B":
            num+=1
if a!=0 and b!=0:
    num+=ab+1
    a-=1
    b-=1
    num+=min(a,b)
elif a!=0:
    num+=ab
elif b!=0:
    num+=ab
else:
    num+=ab-1
print(num)


Fixed code:
n=int(input())
a=0
b=0
ab=0
num=0
for i in range(n):
    s=input()
    if s[0]=="B" and s[-1]=="A":
        ab+=1
    elif s[0]=="B":
        b+=1
    elif s[-1]=="A":
        a+=1
    for j in range(len(s)-1):
        if s[j]=="A" and s[j+1]=="B":
            num+=1
if a!=0 and b!=0:
    num+=ab+1
    a-=1
    b-=1
    num+=min(a,b)
elif a!=0:
    num+=ab
elif b!=0:
    num+=ab
else:
    num+=max(0,ab-1)
print(num)
Buggy code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

a = 0
b = 0
ab = 0
c = 0
for x in s:
    ab += x.count("AB")
    if x[0] == 'B' and x[-1] == 'A':
        c += 1
    elif x[0] == 'B':
        b += 1
    elif x[-1] == 'A':
        a += 1
    
print( ab + max(c-1, 0) + min(a, b) + (1 if a + b != 0 else 0))

Fixed code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

a = 0
b = 0
ab = 0
c = 0
for x in s:
    ab += x.count("AB")
    if x[0] == 'B' and x[-1] == 'A':
        c += 1
    elif x[0] == 'B':
        b += 1
    elif x[-1] == 'A':
        a += 1
    
print( ab + max(c-1, 0) + min(a, b) + (1 if a + b != 0 and c > 0 else 0))
Buggy code:
n=int(input())
ans=0
a=0;b=0;ab=0
for i in range(n):
  s=input()
  ans+=s.count("AB")
  if s[0]=="B":
    b+=1
    if s[-1]=="A":
      ab+=1
      b-=1
  elif s[-1]=="A":
    a+=1
print(ans+c-1+min(a,b))

Fixed code:
n=int(input())
ans=0
a=0;b=0;ab=0
for i in range(n):
  s=input()
  ans+=s.count("AB")
  if s[0]=="B":
    b+=1
    if s[-1]=="A":
      ab+=1
      b-=1
  elif s[-1]=="A":
    a+=1
if a+b==0:
    print(ans+max(ab-1,0))
else:
    print(ans+min(a,b)+ab)