qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N = int(input())
S = (list(input() for i in range(N)))
print(S)
cot = 0
cot_a = 0
cot_b = 0
cot_ab = 0

for s in S:
    if 'AB' in s:
        cot += 1

for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        cot_ab += 1
    elif s[-1] == 'A':
        cot_a += 1
    elif s[0] == 'B':
        cot_b += 1

if cot_ab == 0:
    cot += min(cot_a, cot_b)
elif cot_a + cot_b >0:
    cot += cot_ab + min(cot_a, cot_b)
else:
    cot += cot_ab - 1
print(cot)

Fixed code:
N = int(input())
S = (list(input() for i in range(N)))
cot = 0
cot_a = 0
cot_b = 0
cot_ab = 0

for s in S:
    for j in range(len(s)-1):
        if s[j] == "A" and s[j+1] == "B":
            cot += 1

for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        cot_ab += 1
    elif s[-1] == 'A':
        cot_a += 1
    elif s[0] == 'B':
        cot_b += 1

if cot_ab == 0:
    cot += min(cot_a, cot_b)
elif cot_a + cot_b >0:
    cot += cot_ab + min(cot_a, cot_b)
else:
    cot += cot_ab - 1
print(cot)
Buggy code:
from sys import stdin
n = int(stdin.readline().rstrip())
ans=0
ba=0
bx=0
xa=0
for i in range(n):
  s = stdin.readline().rstrip()
  for j in range(len(s)-1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans +=1
  if( s[0] == 'B' and s[-1] == 'A' ):
    ba +=1
  elif s[0] == 'B':
    bx +=1
  elif s[-1] == 'A':
    xa +=1

ans += min(xa,bx)
if ba>0:
  ans += (ba-1)
  if and xa!=0 and xa!=bx:
    ans += 1

print(ans)

Fixed code:
from sys import stdin
n = int(stdin.readline().rstrip())
ans=0
ba=0
bx=0
xa=0
for i in range(n):
  s = stdin.readline().rstrip()
  for j in range(len(s)-1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans +=1
  if( s[0] == 'B' and s[-1] == 'A' ):
    ba +=1
  elif s[0] == 'B':
    bx +=1
  elif s[-1] == 'A':
    xa +=1

ans += min(xa,bx)
if ba>0:
  ans += (ba-1)
  if (xa+bx)!=0:
    ans += 1

print(ans)
Buggy code:
n = int(input())
s = []
for i in range(n):
    s.append(input())
ans = 0
r = 0
l = 0
a = 0
b = 0
c = 0
for i in range(n):
    ans += s[i].count('AB')
    list = s[i]
    if list[-1] == 'A' and list[0] == 'B':
        r += 1
        l += 1
        c += 1
    elif list[-1] == 'A': # and list[0] != 'B':
        r += 1
        a += 1
    elif list[0] == 'B': # and list[-1] != 'A':
        l += 1
        b += 1
    # print(ans, last, start)
ans += min(r, start)
if a == 0 and b == 0 and c > 0:
    ans -= 1
print(ans)

Fixed code:
n = int(input())
s = []
for i in range(n):
    s.append(input())
ans = 0
r = 0
l = 0
a = 0
b = 0
c = 0
for i in range(n):
    ans += s[i].count('AB')
    list = s[i]
    if list[-1] == 'A' and list[0] == 'B':
        r += 1
        l += 1
        c += 1
    elif list[-1] == 'A': # and list[0] != 'B':
        r += 1
        a += 1
    elif list[0] == 'B': # and list[-1] != 'A':
        l += 1
        b += 1
    # print(ans, last, start)
ans += min(r, l)
if a == 0 and b == 0 and c > 0:
    ans -= 1
print(ans)
Buggy code:
n = int(input())
s = [input() for _ in range(n)]
ans = 0
w = 0
a = 0
b = 0

for c in s:
    ans += c.count("AB")
    if c[0] == "B" and c[-1] == "A":
        w += 1
    elif c[0] == "B":
        b += 1
    elif c[-1] == "A":
        a += 1

if a == b == 0:
    w -= 1
ans += min(w,0) + min(a,b)
print(ans)

Fixed code:
n = int(input())
s = [input() for _ in range(n)]
ans = 0
w = 0
a = 0
b = 0

for c in s:
    ans += c.count("AB")
    if c[0] == "B" and c[-1] == "A":
        w += 1
    elif c[0] == "B":
        b += 1
    elif c[-1] == "A":
        a += 1

if a == b == 0:
    w -= 1
ans += max(w,0) + min(a,b)
print(ans)
Buggy code:
n = int(input())
s = [input() for i in range(n)]

ab = 0
ba = 0
c2 = 0
c3 = 0
for x in s:
    if 'AB' in x:
        ab += x.count('AB')    
    if x[-1] == 'A' and x[0] == 'B':
        ba = 1
    if x[-1] == 'A' and x[0] != 'B':
        c2 += 1
    if x[-1] != 'A' and x[0] == 'B':
        c3 += 1
if ba > 0:
    if c2 + c3 > 0:
        ans = ab + ba + min(c2, c3)
    else: # c2 = c3 =0
        ans = ab + ba - 1
print(ans)


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

ab = 0
ba = 0
c2 = 0
c3 = 0
for x in s:
    if 'AB' in x:
        ab += x.count('AB')    
    if x[-1] == 'A' and x[0] == 'B':
        ba += 1
    if x[-1] == 'A' and x[0] != 'B':
        c2 += 1
    if x[-1] != 'A' and x[0] == 'B':
        c3 += 1
if ba > 0:
    if c2 + c3 > 0:
        ans = ab + ba + min(c2, c3)
    else: # c2 = c3 =0
        ans = ab + ba - 1
else:
    ans = ab + min(c2, c3)
print(ans)
Buggy code:
N=int(input())
A=0
B=0
ans=0
AB=0
for i in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0]=="B" and S[-1]=="A":
        AB+=1
    elif S[0]=="B":
        B+=1
    elif S[-1]=="A":
        A+=1
C=min(A,B)
if A==0 and B==0:
    print(ans+max(0,AB-1))
elif C==0:
    print(ans+AB)

Fixed code:
N=int(input())
A=0
B=0
ans=0
AB=0
for i in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0]=="B" and S[-1]=="A":
        AB+=1
    elif S[0]=="B":
        B+=1
    elif S[-1]=="A":
        A+=1
C=min(A,B)
if A==0 and B==0:
    print(ans+max(0,AB-1))
elif C==0:
    print(ans+AB)
else:
    print(ans+AB+min(A,B))
Buggy code:
n = int(input())

a_end = 0
b_start = 0
a_b = 0
cnt = 0

for _ in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        a_b += 1
        continue
    if s[0] == "B":
        b_start += 1
    if s[-1] == "A":
        a_end += 1

if a_end == 0 and b_start == 0:
    tmp = max(a_b - 1, 0)
else:
    tmp = min(a_end + b_start) + a_b
print(cnt + tmp)

Fixed code:
n = int(input())

a_end = 0
b_start = 0
a_b = 0
cnt = 0

for _ in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        a_b += 1
        continue
    if s[0] == "B":
        b_start += 1
    if s[-1] == "A":
        a_end += 1

if a_end == 0 and b_start == 0:
    tmp = max(a_b - 1, 0)
else:
    tmp = min(a_end,  b_start) + a_b
print(cnt + tmp)
Buggy code:
from collections import Counter

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

cnt=0
cnt_a=0
cnt_b=0
cnt_ab=0
for i in range(n):
    if s[i][0]=='B' and s[i][-1]=='A':
        cnt_ab+=1
    elif s[i][0]=='B':
        cnt_b+=1
    elif s[i][-1]=='A':
        cnt_a+=1
    cnt+=s[i].count('AB')
if cnt_ab=0:
    ans=cnt+min(cnt_a,cnt_b)
elif:
    if cnt_a=0 and cnt_b=0:
        ans=cnt+cnt_ab-1
    else:
        ans=cnt+min(cnt_a,cnt_b)

print(ans)


Fixed code:
from collections import Counter

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

cnt=0
cnt_a=0
cnt_b=0
cnt_ab=0
for i in range(n):
    if s[i][0]=='B' and s[i][-1]=='A':
        cnt_ab+=1
    elif s[i][0]=='B':
        cnt_b+=1
    elif s[i][-1]=='A':
        cnt_a+=1
    cnt+=s[i].count('AB')
if cnt_ab==0:
    ans=cnt+min(cnt_a,cnt_b)
else:
    if cnt_a==0 and cnt_b==0:
        ans=cnt+cnt_ab-1
    else:
        ans=cnt+cnt_ab+min(cnt_a,cnt_b)

print(ans)
Buggy code:
N = int(input())
AB_number = 0
ab_cnt =0
a_cnt = 0
b_cnt = 0
for _ in range(N):
    s_i = input()
    AB_number += s_i.count("AB")
    if s_i[0] == "B" and s_i[-1] == "A":
        ab_cnt += 1
    elif s_i[-1] == "A":
        a_cnt += 1
    elif s_i[0] == "B":
        b_cnt += 1

if ab_cnt== 0:
    print(AB_number+min(a_cnt,b_cnt))
    print(AB_number+min(a_cnt,b_cnt)-1)
elif a_cnt != b_cnt:
    print(AB_number+(ab_cnt-1)+1+min(a_cnt,b_cnt))
else:
    print(AB_number+(ab_cnt-1)+min(a_cnt,b_cnt))


Fixed code:
N = int(input())
AB_number = 0
ab_cnt =0
a_cnt = 0
b_cnt = 0
for _ in range(N):
    s_i = input()
    AB_number += s_i.count("AB")
    if s_i[0] == "B" and s_i[-1] == "A":
        ab_cnt += 1
    elif s_i[-1] == "A":
        a_cnt += 1
    elif s_i[0] == "B":
        b_cnt += 1

if ab_cnt== 0:
    print(AB_number+min(a_cnt,b_cnt))
elif a_cnt != b_cnt:
    print(AB_number+(ab_cnt-1)+1+min(a_cnt,b_cnt))
elif a_cnt==0:
    print(AB_number+(ab_cnt-1)+min(a_cnt,b_cnt))
else:
    print(AB_number+(ab_cnt-1)+1+min(a_cnt,b_cnt))
Buggy code:
N=int(input())
btoa=0
enda=0
startb=0
ab=0
for i in range(N):
	buf=input()
	if buf[0]=="B" and buf[-1]=="A":
		btoa+=1
	elif buf[-1]=="A":
		enda+=1
	elif buf[0]=="B":
		startb+=1
	ab+=buf.count("AB")
if btoa>=1:
	res=ab+btoa-1
else:
	res=ab
if enda!=0 or startb!=0:
	enda+=1
	startb+=1
	print(res+min(enda,startb))
else:
	print(res)

Fixed code:
N=int(input())
btoa=0
enda=0
startb=0
ab=0
for i in range(N):
	buf=input()
	if buf[0]=="B" and buf[-1]=="A":
		btoa+=1
	elif buf[-1]=="A":
		enda+=1
	elif buf[0]=="B":
		startb+=1
	ab+=buf.count("AB")
if btoa>=1:
	res=ab+btoa-1
	if enda!=0 or startb!=0:
		enda+=1
		startb+=1
		print(res+min(enda,startb))
	else:
		print(res)
else:
	print(ab+min(enda,startb))
Buggy code:
n = int(input())

_a = 0
b_ = 0
b_a = 0

cnt = 0

for i in range(n):
    s = input()

    sa = False
    sb = False
    if s[0] == "B":
        sb = True
    if s[-1] == "A":
        sa = True

    if sa and sb:
        b_a += 1
    elif sa:
        _a += 1
    elif sb:
        b_ += 1

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

if b_a > 0:
    cnt += b_a - 1
    if _a > 1:
        _a -= 1
        cnt += 1
    if b_ > 1:
        b_ -= 1
        cnt += 1

cnt += min(_a, b_)
print(cnt)

Fixed code:
n = int(input())

_a = 0
b_ = 0
b_a = 0

cnt = 0

for i in range(n):
    s = input()

    sa = False
    sb = False
    if s[0] == "B":
        sb = True
    if s[-1] == "A":
        sa = True

    if sa and sb:
        b_a += 1
    elif sa:
        _a += 1
    elif sb:
        b_ += 1

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

if b_a > 0:
    cnt += b_a - 1
    if _a >= 1:
        _a -= 1
        cnt += 1
    if b_ >= 1:
        b_ -= 1
        cnt += 1

cnt += min(_a, b_)
print(cnt)
Buggy code:
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss]
headcount = sum([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = sum([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = sum([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if outcount==0 and headcount==0:
    print(sum(count)+multicount-1)
else:
    print(sum(count)+min(headcount,outcount)+multicount)

Fixed code:
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss]
headcount = len([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = len([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = len([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if outcount==0 and headcount==0:
    if multicount<=0:
        print(sum(count))
    else:
        print(sum(count)+multicount-1)
else:
    print(sum(count)+min(headcount,outcount)+multicount)
Buggy code:
#coding: utf-8
N = int(input())
ans = 0
l_B = 0
r_A = 0
lbra = 0
for _ in range(N):
	tmp = input()
	ans += tmp.count("AB")
	if tmp[0] == "B":
		if tmp[-1] == "A":
			lbra += 1
		else:
			l_B += 1
	elif tmp[-1] == "A":
		r_A += 1
l.sort()
print(ans + lbra + min(l_B, r_A))

Fixed code:
#coding: utf-8
N = int(input())
ans = 0
l_B = 0
r_A = 0
lbra = 0
for _ in range(N):
	tmp = input()
	ans += tmp.count("AB")
	if tmp[0] == "B":
		if tmp[-1] == "A":
			lbra += 1
		else:
			l_B += 1
	elif tmp[-1] == "A":
		r_A += 1
        
if lbra == 0:
    ans += min(l_B, r_A)
else:
    if l_B + r_A > 0:
        ans += lbra + min(l_B, r_A)
    else:
        ans += lbra - 1
print(ans)
Buggy code:
N=int(input())
ba=bx=xa=0
ans=0
for i in range(N):
    s=input()
    ans+=s.count('AB')
    if s.startswith('B') and s.endswith('A'):
        ba+=1
    elif s.startswith('B'):
        bx+=1
    elif s.endswith('A'):
        xa+=1

if ba==0:
    ans+=min(bx,xa)
elif ba>0:
    if bx+xa>0:
        ans+=ba+min(bx,xa)
    else bx+xa==0
        ans+=ba-1

print(ans)

Fixed code:
N=int(input())
ba=bx=xa=0
ans=0
for i in range(N):
    s=input()
    ans+=s.count('AB')
    if s.startswith('B') and s.endswith('A'):
        ba+=1
    elif s.startswith('B'):
        bx+=1
    elif s.endswith('A'):
        xa+=1

if ba==0:
    ans+=min(bx,xa)
elif ba>0:
    if bx+xa>0:
        ans+=ba+min(bx,xa)
    elif bx+xa==0:
        ans+=ba-1

print(ans)
Buggy code:
N = int(input())
count = 0
end = 0
start = 0
double = 0
for i in range(N):
    line = input()
    if "AB" in line:
        count += 1
    if line[0] == "B":
        if line[-1] == "A":
            double +=1
        else:
            start += 1
    if line[-1] == "A":
        if line[0] == "B":
            double +=1
        else:
            end += 1
if double == 0:
    count += min(start, end)
elif start + end > 0:
    counr += double + min(start, end)
elif start + end == 0:
    count += double - 1
print(count)
        

Fixed code:
N = int(input())
count = 0
end = 0
start = 0
double = 0
for i in range(N):
    line = input()
    if "AB" in line:
        count += line.count("AB")
    if line[0] == "B":
        if line[-1] == "A":
            double +=1
        else:
            start += 1
    elif line[-1] == "A":
            end += 1
if double == 0:
    count += min(start, end)
elif start + end > 0:
    count += double + min(start, end)
elif start + end == 0:
    count += double - 1
print(count)
Buggy code:
# N < 10000
N = int(input())

# len(s) < 10

count = 0
a, b, ba = 0, 0, 0
for i in range(N):
    s = input().rstrip()
    count += s.count("AB")

    if s[-1] == 'A':
        if s[0] == 'B':
            ba += 1
        else:
            a += 1
    elif s[0] == 'B':
        b += 1

#e = int(input())
#print("*A=",a, ", B*=",b, "B*A=",ba, ",AB=", count, ",expected=", e)

if ba > 1:
    count += ba - 1
    ba = 1

if ba == 1:
    c0 = 1 + min(a, b)
count+=max(min(a+ba,b),min(a,b+ba), c0)
print(count)


Fixed code:
# N < 10000
N = int(input())

# len(s) < 10

count = 0
a, b, ba = 0, 0, 0
for i in range(N):
    s = input().rstrip()
    count += s.count("AB")

    if s[-1] == 'A':
        if s[0] == 'B':
            ba += 1
        else:
            a += 1
    elif s[0] == 'B':
        b += 1

#e = int(input())
#print("*A=",a, ", B*=",b, "B*A=",ba, ",AB=", count, ",expected=", e)

if ba > 1:
    count += ba - 1
    ba = 1

if a + b > 0:
    #count += max(min(a + ba, b),min (a, b + ba))
    count += min(a,b) + ba
print(count)
Buggy code:
N = int(input())
count, A, B, BA = 0, 0, 0, 0
for _ in range(N):
    s = input()
    count += s.count('AB')
    B += s[0] == 'B'
    A += s[-1] == 'A'
    BA += s[0] == 'B' and s[-1] == 'A'
    print(s, A,B,BA, 'count', count)
tmp = (A - 1) if (A == B and A == BA and BA != 0) else min(A,B)
print(count + tmp)


Fixed code:
N = int(input())
count, A, B, BA = 0, 0, 0, 0
for _ in range(N):
    s = input()
    count += s.count('AB')
    B += s[0] == 'B' and s[-1] != 'A'
    A += s[-1] == 'A' and s[0] != 'B'
    BA += s[0] == 'B' and s[-1] == 'A'
    #print(s, A,B,BA, 'count', count)

if BA == 0 :
    tmp = min(A,B)
else:
    if A + B > 0:
        tmp = BA + min(A,B)
    else:
        tmp = BA - 1
print(count + tmp)
Buggy code:
n=int(input())
s=[]
ctr=0
ctr_a=0
ctr_b=0
ctr_ab=0
for i in range(n):
    s.append(str(input()))
    if "AB" in s[i]:
        ctr+=s[i].count("AB")
    if s[i][-1]=="A" and s[i][0]!="B":
        ctr_a+=1
    if s[i][0]=="B" and s[i][-1]!="A":
        ctr_b+=1
    if s[i][-1]=="A" and s[i][0]=="B":
        ctr_ab+=1
if ctr_a==ctr_b==0:
    print(max(ctr+ctr_ab-1,0))
else:
    print(ctr+min(ctr_a,ctr_b))


Fixed code:
n=int(input())
s=[]
ctr=0
ctr_a=0
ctr_b=0
ctr_ab=0
for i in range(n):
    s.append(str(input()))
    if "AB" in s[i]:
        ctr+=s[i].count("AB")
    if s[i][-1]=="A" and s[i][0]!="B":
        ctr_a+=1
    if s[i][0]=="B" and s[i][-1]!="A":
        ctr_b+=1
    if s[i][-1]=="A" and s[i][0]=="B":
        ctr_ab+=1
if ctr_ab==0:
    print(ctr+min(ctr_a,ctr_b))
else:
    if ctr_a+ctr_b>0:
        print(ctr+min(ctr_a,ctr_b)+ctr_ab)
    else:
        print(ctr+ctr_ab-1)
Buggy code:
n = int(input())
count = 0
l = [input() for i in range(n)]
a = 0
b = 0
ab = 0
for i in range(n):
    if "AB" in l[i]:
        count += l[i].count("AB")
    if l[i][0] == "B" and l[i][-1] == "A":
        ab += 1
    elif l[i][0] == "B":
        b += 1
    elif l[i][-1] == "A":
        a += 1
x= min(ab,b)
count += x
b -= x
x= min(ab,a)
count += x
a -= x
if a and b:
    count += min(a,b)
print(count)



Fixed code:
n = int(input())
count = 0
l = [input() for i in range(n)]
a = 0
b = 0
ab = 0
for i in range(n):
    if "AB" in l[i]:
        count += l[i].count("AB")
    if l[i][0] == "B" and l[i][-1] == "A":
        ab += 1
    elif l[i][0] == "B":
        b += 1
    elif l[i][-1] == "A":
        a += 1
if ab >= 2:
    count += ab-1
    ab = 1
x= min(ab,b)
count += x
b -= x
x= min(ab,a)
count += x
a -= x
if a and b:
    count += min(a,b)
print(count)
Buggy code:
s = [input() for _ in range(int(input()))]
a = b = ab = ans = 0
for si in s:
    if si[0] == "B":
        b += 1
    for j in range(len(si)-1):
        if si[j] == "A" and si[j+1] == "B":
            ans += 1
    if si[-1] == "A":
        a += 1
    if si[0] == "B" and si[-1] == "A":
        ab += 1
print(ans + max(ab-1, 0)) if a == b == 0 else print(ans + ab + min(a, b))

Fixed code:
s = [input() for _ in range(int(input()))]
a = b = ab = ans = 0
for si in s:
    for j in range(len(si)-1):
        if si[j] == "A" and si[j+1] == "B":
            ans += 1
    if si[0] == "B" and si[-1] == "A":
        ab += 1
    elif si[0] == "B":
        b += 1
    elif si[-1] == "A":
        a += 1
print(ans + max(ab-1, 0)) if a == b == 0 else print(ans + ab + min(a, b))
Buggy code:
n = int(input())
l = [input() for i in range(n)]
a = 0
b = 0
ba = 0
ab = 0
for i in range(n):
    if 'AB' in l[i]:
        for j in range(len(l[i])-1):
            if l[i][j] == 'A' and l[i][j+1] == 'B':
                ab += 1
    if l[i][0] =='B' and l[i][-1] == 'A':
        ba += 1
    elif l[i][-1] == 'A':
        a += 1
    elif l[i][0] == 'B':
        b += 1
if max(a,b) == 0　and ba <= 1:
    print(ab)
elif max(a,b) == 0
    print((ab+ba-1))
else:
    print(min(a,b)+ab+ba)

Fixed code:
n = int(input())
l = [input() for i in range(n)]
a = 0
b = 0
ba = 0
ab = 0
for i in range(n):
    if 'AB' in l[i]:
        for j in range(len(l[i])-1):
            if l[i][j] == 'A' and l[i][j+1] == 'B':
                ab += 1
    if l[i][0] =='B' and l[i][-1] == 'A':
        ba += 1
    elif l[i][-1] == 'A':
        a += 1
    elif l[i][0] == 'B':
        b += 1
if max(a,b) == 0 and ba <= 1:
    print(ab)
elif max(a,b) == 0:
    print(ab+ba-1)
else:
    print(min(a,b)+ab+ba)
Buggy code:
n = int(input())
s = []
for i in range(n):
    s.append(input())
out = 0
a=0
b=0
c=0
for i in range(len(s)):
    if s[i][0] == "B" and s[i][len(s[i])-1] == "A":
        a=a+1
    elif s[i][0] == "B" and s[i][len(s[i])-1] != "A":
        b=b+1
    elif s[i][0] != "B" and s[i][len(s[i])-1] == "A":
        c=c+1
    for j in range(len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            out = out+1
if a== 0 and b == 0 and c == 0:
elif b == 0 and c == 0:
    out = out + a - 1
else :
    out = out +a+ min(b,c)
    
print(int(out))

Fixed code:
n = int(input())
s = []
for i in range(n):
    s.append(input())
out = 0
a=0
b=0
c=0
for i in range(len(s)):
    if s[i][0] == "B" and s[i][len(s[i])-1] == "A":
        a=a+1
    elif s[i][0] == "B" and s[i][len(s[i])-1] != "A":
        b=b+1
    elif s[i][0] != "B" and s[i][len(s[i])-1] == "A":
        c=c+1
    for j in range(len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            out = out+1
if a== 0 and b == 0 and c == 0:
    out = out
elif b == 0 and c == 0:
    out = out + a - 1
else :
    out = out +a+ min(b,c)
    
print(int(out))
Buggy code:
n = int(input())
c = 0
xa = 0
bx = 0
ba = 0
for _ in range(n):
  si = str(input())
  l = len(si)
  s1 = si[0]
  s2 = si[-1]
  if s1=="B":
    if s2=="A":
      ba += 1
    else:
      bx += 1
  elif s2 == "A":
    xa += 1
  for i in range(l-1):
    if si[i]=="A" and si[i+1]=="B":
      c += 1
print(c,ba,bx,xa)
if ba == 0:
  print(c+min(bx,xa))
  exit()
if bx == 0:
  if  xa == 0:
    print(ba-1+c)
    exit()
  else:
    print(ba+c)
    exit()
if xa == 0:
  print(ba+c)
  exit()
print(min(bx,xa)+ba+c)

Fixed code:
n = int(input())
c = 0
xa = 0
bx = 0
ba = 0
for _ in range(n):
  si = str(input())
  l = len(si)
  s1 = si[0]
  s2 = si[-1]
  if s1=="B":
    if s2=="A":
      ba += 1
    else:
      bx += 1
  elif s2 == "A":
    xa += 1
  for i in range(l-1):
    if si[i]=="A" and si[i+1]=="B":
      c += 1
if ba == 0:
  print(c+min(bx,xa))
  exit()
if bx == 0:
  if  xa == 0:
    print(ba-1+c)
    exit()
  else:
    print(ba+c)
    exit()
if xa == 0:
  print(ba+c)
  exit()
print(min(bx,xa)+ba+c)
Buggy code:
N=int(input())
s=0
BA=0
XA=0
BX=0
for i in range(N):
    st=input()
    s+=st.count("AB")
    f1=0
    f2=0
    if(st[0]=="B"):f1=1
    if(st[-1]=="A"):f2=1

    if(f1 and f2):
        BA+=1
    elif(f1):
        BX+=1
    elif(f2):
        XA+=1

print(s+min(BX+BA,BX+BA))

Fixed code:
N=int(input())
s=0
BA=0
XA=0
BX=0
for i in range(N):
    st=input()
    s+=st.count("AB")
    f1=0
    f2=0

    
    if(st[0]=="B"):f1=1
    if(st[-1]=="A"):f2=1

    if(f1 and f2):
        BA+=1
    elif(f1):
        BX+=1
    elif(f2):
        XA+=1
if(XA==0 and BX==0 and BA<2):
    print(s)
elif(XA==0 and BX==0):
    print(s+BA-1)
else:
    print(s+min(XA+BA,BX+BA))
Buggy code:
N = int(input())
inward = 0
top = 0
fin = 0
dbl = 0

for _ in range(N):
    s = input()
    n = len(s)
    if s[0] == 'B' and s[n-1] == 'A':
        dbl += 1
    elif s[0] == 'B' and s[n-1] != 'A':
        top += 1
    elif s[0] != 'B' and s[n-1] == 'A':
        fin += 1
    for i in range(n-1):
        if s[i] == 'A' and s[i+1] == 'B':
            inward += 1

ans = inward
MAX = N-1
if dbl>0:
    ans += dbl-1
    MAX -= (dbl-1)
if top >0:
    ans +=1
    top -=1
    MAX -=1
if fin >0:
    ans +=1
    fin -=1
    MAX -=1
ans += min(top,fin,MAX)
print(ans)

Fixed code:
N = int(input())
inward = 0
top = 0
fin = 0
dbl = 0

for _ in range(N):
    s = input()
    n = len(s)
    if s[0] == 'B' and s[n-1] == 'A':
        dbl += 1
    elif s[0] == 'B' and s[n-1] != 'A':
        top += 1
    elif s[0] != 'B' and s[n-1] == 'A':
        fin += 1
    for i in range(n-1):
        if s[i] == 'A' and s[i+1] == 'B':
            inward += 1

ans = inward
MAX = N-1
if dbl>0:
    ans += dbl-1
    MAX -= (dbl-1)
    if top >0:
        ans +=1
        top -=1
        MAX -=1
    if fin >0:
        ans +=1
        fin -=1
        MAX -=1
ans += min(top,fin,MAX)
print(ans)
Buggy code:
N = int(input())

a, b, c, d = 0, 0, 0, 0
for _ in range(N):
    s = input()
    if s[0]=="B" and s[-1]=="A":
        a += 1
    elif s[0]=="B":
        b += 1
    elif s[-1]=="A":
        c += 1
    d += s.count("AB")

ans = d + max(0, a-1) + min(b, c)
if b!=c and a and b and c:
    ans += 1
print(ans)

Fixed code:
N = int(input())

a, b, c, d = 0, 0, 0, 0
for _ in range(N):
    s = input()
    if s[0]=="B" and s[-1]=="A":
        a += 1
    elif s[0]=="B":
        b += 1
    elif s[-1]=="A":
        c += 1
    d += s.count("AB")

ans = d
if b==c==0:
    ans += max(0, a-1)
else:
    ans += a + min(b, c)
print(ans)
Buggy code:
n=int(input())
b_start=0
a_last=0
both=0
ans=0
for i in range(n):
    s=input()
    ans+=s.count("AB")
    if s[0]=="B":
        if s[len(s)-1]=="A":
            both+=1
        else:
            b_start+=1
    elif s[len(s)-1]=="A":
        a_last+=1
diff=abs(b_start-a_last)
print(a_last, b_start, both, ans, diff)
ans+=both-1
if a_last>=1:
    ans+=1
    a_last-=1
if b_start>=1:
    ans+=1
    b_start-=1
ans+=min(a_last,b_start)
print(ans)
# if diff==0:
#     print(1)
#     print(max(b_start,a_last)+max(0,(both-1))+ans)
# elif diff >= both:
#     print(2)
#     print(min(b_start,a_last)+both+ans)
# else:
#     print(3)
#     print(max(b_start,a_last)+(both-diff)+ans)


Fixed code:
n=int(input())
b_start=0
a_last=0
both=0
ans=0
for i in range(n):
    s=input()
    ans+=s.count("AB")
    if s[0]=="B":
        if s[len(s)-1]=="A":
            both+=1
        else:
            b_start+=1
    elif s[len(s)-1]=="A":
        a_last+=1
diff=abs(b_start-a_last)
# print(a_last, b_start, both, ans, diff)
if both>=1:
    ans+=both-1
    if a_last>=1:
        ans+=1
        a_last-=1
    if b_start>=1:
        ans+=1
        b_start-=1
ans+=min(a_last,b_start)
print(ans)