qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n=int(input())
s=[]
for i in range(n):
  s.append(input())
cnt_a=0
cnt_b=0
for j in range(n):
  if s[j][0]=='B' and s[j][len(s[j])-1]=='A':
    cnt_a+=1
    cnt_b+=1
  elif s[j][0]=='B':
    cnt_b+=1
  elif s[j][len(s[j])-1]=='A':
    cnt_a+=1
cnt_2=0
for l in range(n):
  cnt_2+=s[l].count('AB')
if cnt_a==cnt_b:
  print((cnt_a+cnt_2)-1)
else:
  print(min(cnt_a,cnt_b)+cnt_2)
    
    
    
    
                 
  


Fixed code:
n=int(input())
s=[]
for i in range(n):
  s.append(input())
cnt_a=0
cnt_b=0
cnt_ab=0
for j in range(n):
  if s[j][0]=='B' and s[j][len(s[j])-1]=='A':
    cnt_ab+=1
  elif s[j][0]=='B':
    cnt_b+=1
  elif s[j][len(s[j])-1]=='A':
    cnt_a+=1
cnt_a+=cnt_ab
cnt_b+=cnt_ab
cnt_2=0
for l in range(n):
  cnt_2+=s[l].count('AB')
if cnt_a==0 and cnt_b==0:
  print(cnt_2)
elif cnt_ab-cnt_a==0 and cnt_ab-cnt_b==0:
  print((cnt_ab+cnt_2)-1)
else:
  print(min(cnt_a,cnt_b)+cnt_2)
Buggy code:
n = int(input())
cnt = 0
s0b = 0
sla = 0
s0bsla = 0
for i in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] != "A":
        s0b += 1
    elif s[0] != "B" and s[-1] == "A":
        sla += 1
    elif s[0] == "B" and s[-1] == "A":
        s0bsla += 1
if s0bsla > 0:
    cnt += s0bsla - 1
if sla > 0:
    cnt += 1
    sla -= 1
if s0b > 0:
    cnt += 1
    s0b -= 1
cnt += min(s0b, sla)
print(cnt)


Fixed code:
n = int(input())
cnt = 0
s0b = 0
sla = 0
s0bsla = 0
for i in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] != "A":
        s0b += 1
    elif s[0] != "B" and s[-1] == "A":
        sla += 1
    elif s[0] == "B" and s[-1] == "A":
        s0bsla += 1
if s0bsla > 0:
    cnt += s0bsla - 1
    if sla > 0:
        cnt += 1
        sla -= 1
    if s0b > 0:
        cnt += 1
        s0b -= 1
cnt += min(s0b, sla)
print(cnt)
Buggy code:
n=int(input())
s=[input() for i in range(n)]
lastA=0
firstB=0
lastAandfirstB=0
ABs=0
for si in s:
    ABs+=si.count("AB")
    firstB+=(si[0]=="B")
    lastA+=(si[-1]=="A")
    lastAandfirstB+=(si[0]=="B")*(si[-1]=="A")

can=min(firstB,lastA)
print(lastA,firstB,lastAandfirstB)
if lastAandfirstB==1 and firstB==1 and lastA==1:
    print(ABs)
    exit()
if can==n or lastAandfirstB==firstB==lastA:
    can=can-1
print(can+ABs)

Fixed code:
n=int(input())
s=[input() for i in range(n)]
lastA=0
firstB=0
lastAandfirstB=0
ABs=0
for si in s:
    ABs+=si.count("AB")
    firstB+=(si[0]=="B")
    lastA+=(si[-1]=="A")
    lastAandfirstB+=(si[0]=="B")*(si[-1]=="A")

can=min(firstB,lastA)
# print(lastA,firstB,lastAandfirstB)
# if lastAandfirstB==1 and firstB==1 and lastA==1:
#     print(ABs)
#     exit()
if can==n or lastAandfirstB==firstB==lastA!=0:
    can=can-1
print(can+ABs)
Buggy code:
import sys

def inp():
  return sys.stdin.readline().strip()

N = int(inp())
inAB = 0
sB = 0
eA = 0
both = 0
for _ in range(N):
  s = inp()
  inAB += s.count('AB')
  if s[0] == 'B' and s[-1] == 'A':
    both += 1
    continue
  if s[0] == 'B':
    sB += 1
  if s[-1] == 'A':
    eA += 1

pair = min(eA, sB) if min(eA, sB) > 0 else -1
print(inAB + max(both + pair, 0))

Fixed code:
import sys

def inp():
  return sys.stdin.readline().strip()

N = int(inp())
inAB = 0
sB = 0
eA = 0
both = 0
for _ in range(N):
  s = inp()
  inAB += s.count('AB')
  if s[0] == 'B' and s[-1] == 'A':
    both += 1
    continue
  if s[0] == 'B':
    sB += 1
  if s[-1] == 'A':
    eA += 1

pair = min(eA, sB)
if sB > 0 or eA > 0:
  ans = both + pair
else:
  ans = both - 1
print(inAB + max(ans, 0))
Buggy code:
def main():
    n = int(input())
    s = [input() for i in range(n)]

    AB = 0
    B_ = 0
    _A = 0
    B_A = 0
    for ss in s:
        for i in range(len(ss)-1):
            if ss[i:i+2] == 'AB':
                AB += 1
        if ss[0] == 'B':
            if ss[-1] == 'A':
                B_A += 1
            else:
                B_ += 1
        elif ss[-1] == 'A':
            _A += 1
    if _A == B_ && _A > 1:
        print(AB + max((B_A - 1), 0) + max(min(B_ + int(B_A > 0), _A), min(B_, _A + int(B_A > 0))) + 1)
    else:
        print(AB + max((B_A - 1), 0) + max(min(B_ + int(B_A > 0), _A), min(B_, _A + int(B_A > 0))))
    # print(AB, B_, _A, B_A)

if __name__ == '__main__':
    main()


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

    AB = 0
    B_ = 0
    _A = 0
    B_A = 0
    for ss in s:
        for i in range(len(ss)-1):
            if ss[i:i+2] == 'AB':
                AB += 1
        if ss[0] == 'B':
            if ss[-1] == 'A':
                B_A += 1
            else:
                B_ += 1
        elif ss[-1] == 'A':
            _A += 1
    if B_A > 0:
        if _A > 0 and B_ > 0:
            print(AB + (B_A - 1) + 2 + min(_A - 1, B_ - 1))
        elif _A == 0 and B_ == 0:
            print(AB + (B_A - 1))
        else:
            print(AB + (B_A - 1) + 1)
    else:
        print(AB + min(_A, B_))

if __name__ == '__main__':
    main()
Buggy code:
x = int(input())
a = 0
b = 0
c = 0
f = 1
bun = []
for p in range(x):
    bun = input()
    if bun[0] == "B":
        b +=1
    if bun[len(bun)-1] == "A":
        a += 1
        if bun[0] == "A":
            f = 0
    for k in range(len(bun)-1):
        if bun[k] == "A":
            if bun[k+1] == "B":
                c += 1
    
if a == x:
    a = a-1
if a < b:
    print(a+c-f)
else:
    print(b+c-f)

Fixed code:
x = int(input())
a = 0
b = 0
c = 0
f = 0
bun = []
for p in range(x):
    bun = input()
    if bun[0] == "B":
        b +=1
    if bun[len(bun)-1] == "A":
        a += 1
        if bun[0] == "B":
            f += 1
    for k in range(len(bun)-1):
        if bun[k] == "A":
            if bun[k+1] == "B":
                c += 1



if f == a:
    if a == b:
        if a > 0:
            a -= 1
if a < b:
    print(a+c)
else:
    print(b+c)
Buggy code:
N=int(input())
ans=0
A=0
BA=0
B=0
for i in range(N):
    S=list(input())
    for j in range(1,len(S)):
        if S[j-1]=="A" and S[j]=="B":
            ans+=1
    if S[0]=="B" and S[len(S)-1]=="A":
        BA+=1
    elif S[0]=="B":
        B+=1
    elif S[len(S)-1]=="A":
        A+=1
print(ans,A,B,BA)
if A>=1 or B>=1:
    print(ans+BA+min(A,B))
elif BA>=1:
    print(ans+BA-1)
else:
    print(ans)


Fixed code:
N=int(input())
ans=0
A=0
BA=0
B=0
for i in range(N):
    S=list(input())
    for j in range(1,len(S)):
        if S[j-1]=="A" and S[j]=="B":
            ans+=1
    if S[0]=="B" and S[len(S)-1]=="A":
        BA+=1
    elif S[0]=="B":
        B+=1
    elif S[len(S)-1]=="A":
        A+=1
#print(ans,A,B,BA)
if A>=1 or B>=1:
    print(ans+BA+min(A,B))
elif BA>=1:
    print(ans+BA-1)
else:
    print(ans)
Buggy code:
n = int(input())
s = [input() for _ in range(n)]

cnt = 0
cnta = 0
cntb = 0
cntab = 0
for i in range(n):
    if 'AB' in s[i]:
        cnt += s[i].count('AB')
    if s[i][0] == 'B' and s[i][-1] == 'A':
        cntab += 1
    elif s[i][0] == 'B':
        cntb += 1
    elif s[i][-1] == 'A':
        cnta += 1
    
if max(cnta, cntb) == cntab: cnta -= 1
cnt += min(cnta, cntb)
print(cnt)

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

cnt = 0
cnta = 0
cntb = 0
cntab = 0
for i in range(n):
    if 'AB' in s[i]:
        cnt += s[i].count('AB')
    if s[i][0] == 'B' and s[i][-1] == 'A':
        cntab += 1
    elif s[i][0] == 'B':
        cntb += 1
    elif s[i][-1] == 'A':
        cnta += 1
    
if cntab == 0:
    cnt += min(cnta, cntb)
else:
    if cnta + cntb > 0:
        cnt += cntab + min(cnta, cntb)
    else:
        cnt += cntab - 1
print(cnt)
Buggy code:
n=int(input())
S=[list(input())for i in range(n)]
A_cnt=0
B_cnt=0
AB_cnt=0
for s in S:
        if S[0]=="B" and S[-1]=="A":
                AB_cnt+=1
        elif s[-1]=="A":
                A_cnt+=1
        elif s[0]=="B":
                B_cnt+=1

cnt=0
for i in range(n):
        S[i].append("T")
        for j in range(len(S[i])):
                if S[i][j]=="A" and S[i][j+1]=="B":
                        cnt+=1
if A_cnt==B_cnt==0 and 0<AB_cnt:
        print(AB_cnt-1+cnt)
else:
        print(cnt+min(A_cnt,B_cnt))


Fixed code:
n=int(input())
S=[list(input())for i in range(n)]
A_cnt=0
B_cnt=0
AB_cnt=0
for s in S:
        if s[0]=="B" and s[-1]=="A":
                AB_cnt+=1
        elif s[-1]=="A":
                A_cnt+=1
        elif s[0]=="B":
                B_cnt+=1

cnt=0
for i in range(n):
        S[i].append("T")
        for j in range(len(S[i])):
                if S[i][j]=="A" and S[i][j+1]=="B":
                        cnt+=1
if A_cnt==B_cnt==0 and 0<AB_cnt:
        print(AB_cnt-1+cnt)
else:
        print(cnt+min(A_cnt+AB_cnt,B_cnt+AB_cnt))
Buggy code:
import sys
input = sys.stdin.readline

import numpy as np

N = int(input())
S_list = [input().rstrip() for _ in range(N)]

S_ab_number = [i.count('AB') for i in S_list]
S_b_start_number = [1 if i[0] == 'B' else 0 for i in S_list]
S_a_end_number = [1 if i[-1] == 'A' else 0 for i in S_list]
S_both_number = [1 if i[-1] == 'A' and i[0] == 'B' else 0 for i in S_list]

print(sum(S_ab_number) + min((sum(S_b_start_number)-sum(S_both_number),(sum(S_a_end_number)-sum(S_both_number)))))

Fixed code:
import sys
input = sys.stdin.readline

import numpy as np

N = int(input())
S_list = [input().rstrip() for _ in range(N)]

S_ab_number = [i.count('AB') for i in S_list]
S_b_start_number = [1 if i[0] == 'B' and i[-1] != 'A' else 0 for i in S_list]
S_a_end_number = [1 if i[-1] == 'A' and i[0] != 'B' else 0 for i in S_list]
S_both_number = [1 if i[-1] == 'A' and i[0] == 'B' else 0 for i in S_list]

if sum(S_both_number) == 0:
    print(sum(S_ab_number) +  min(sum(S_b_start_number),sum(S_a_end_number)))
elif sum(S_b_start_number) == 0 and sum(S_a_end_number) != 0:
    print(sum(S_ab_number) + sum(S_both_number))
elif sum(S_b_start_number) != 0 and sum(S_a_end_number) == 0:
    print(sum(S_ab_number) + sum(S_both_number))
elif sum(S_b_start_number) == 0 and sum(S_a_end_number) == 0:
    print(sum(S_ab_number) + sum(S_both_number) - 1)
else:
    print(sum(S_ab_number) + sum(S_both_number) + 1 + min(sum(S_b_start_number)-1,sum(S_a_end_number)-1))
Buggy code:
N = int(input())
S = [input() for i in range(N)]

ans = 0
A = B = BA = 0
for s in S:
    for i in range(len(s) - 1):
        if s[i] + s[i + 1] == 'AB':
            ans += 1

    if s[-1] == 'A' and s[0] == 'B':
        BA += 1
    elif s[-1] == 'A':
        A += 1
    elif s[0] == 'B':
        B += 1

if A & B:
    ans += BA + 2
    A, B = A - 1, B - 1
    ans += min(A, B)

elif A:
    ans += BA
elif B:
    ans += BA

print(ans)

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

ans = 0
A = B = BA = 0
for s in S:
    for i in range(len(s) - 1):
        if s[i] + s[i + 1] == 'AB':
            ans += 1

    if s[-1] == 'A' and s[0] == 'B':
        BA += 1
    elif s[-1] == 'A':
        A += 1
    elif s[0] == 'B':
        B += 1


if (A > 0) & (B > 0) & (BA > 0):
    ans += BA + 1
    A, B = A - 1, B - 1
    ans += min(A, B)

elif (A > 0) & (B > 0):
    ans += min(A, B)

elif A > 0:
    ans += BA

elif B > 0:
    ans += BA

elif BA > 0:
    ans += (BA - 1)

print(ans)
Buggy code:
import math
N = int(input())
S = []
counterBX = 0
counterXA = 0
counterBXA = 0
counterAB = 0
for i in range(N):
    S.append(input())
    if S[i][0] == 'B' and S[i][-1] != 'A':
        counterBX +=1
    if S[i][0] != 'B' and S[i][-1] == 'A':
        counterXA +=1
    if S[i][0] == 'B' and S[i][-1] == 'A':
        counterBXA +=1
    if len(S[i]) > 1:
        for j in range(len(S[i]) - 1):
            if S[i][j] == 'A' and S[i][j+1] == 'B':
                counterAB +=1
minval = min(counterBX,counterXA)
sa = abs(counterBX - counterXA)

if counterBXA == 0:
    ans = counterAB + min(counterXA,counterBX)
elif counterBXA >=1 and counterXA == 0 and counterBX == 0:
    ans = counterAB + counterBXA - 1
elif counterBXA >=1 and counterXA >=1 and counterBX == 0:
    ans = counterAB + (counterBXA - 1) + 1 + min(counterXA - 1,counterBX)
elif counterBXA >=1 and counterXA == 0 and counterBX >=1:
    ans = counterAB + (counterBXA - 1) + 1 +min(counterXA,counterBX - 1)
else:
    ans = counter(BXA - 1) + 2 + min(counterXA - 1, counterBX - 1)
print(ans)

Fixed code:
import math
N = int(input())
S = []
counterBX = 0
counterXA = 0
counterBXA = 0
counterAB = 0
for i in range(N):
    S.append(input())
    if S[i][0] == 'B' and S[i][-1] != 'A':
        counterBX +=1
    if S[i][0] != 'B' and S[i][-1] == 'A':
        counterXA +=1
    if S[i][0] == 'B' and S[i][-1] == 'A':
        counterBXA +=1
    if len(S[i]) > 1:
        for j in range(len(S[i]) - 1):
            if S[i][j] == 'A' and S[i][j+1] == 'B':
                counterAB +=1
minval = min(counterBX,counterXA)
sa = abs(counterBX - counterXA)

if counterBXA == 0:
    ans = counterAB + min(counterXA,counterBX)
elif counterBXA >=1 and counterXA == 0 and counterBX == 0:
    ans = counterAB + counterBXA - 1
elif counterBXA >=1 and counterXA >=1 and counterBX == 0:
    ans = counterAB + (counterBXA - 1) + 1 + min(counterXA - 1,counterBX)
elif counterBXA >=1 and counterXA == 0 and counterBX >=1:
    ans = counterAB + (counterBXA - 1) + 1 +min(counterXA,counterBX - 1)
else:
    ans = counterAB + (counterBXA - 1) + 2 + min(counterXA - 1, counterBX - 1)
print(ans)
Buggy code:
N = int(input())

count_ab = 0
S_tail_a_head_b = 0
S_tail_a = 0
S_head_b = 0
for _ in range(N):
    s = input()
    if s.find('AB') == -1 and s[0] != 'B' and s[-1] != 'A':
        continue

    count_ab += s.count('AB')

    if s[0] == 'B' and s[-1] == 'A':
        S_tail_a_head_b += 1
    elif s[0] == 'B':
        S_head_b += 1
    else:  # s[-1] == 'A'
        S_tail_a += 1

if S_tail_a_head_b == 0:
    count_ab += min(S_head_b, S_tail_a)
else:
    if sum([S_tail_a, S_head_b]) > 0:
        count_ab += S_tail_a_head_b + min(S_head_b, S_tail_a)
    else:
        count_ab += S_tail_a_head_b - 1

print(count_ab)


Fixed code:
N = int(input())

count_ab = 0
S_tail_a_head_b = 0
S_tail_a = 0
S_head_b = 0
for _ in range(N):
    s = input()
    count_ab += s.count('AB')

    if s[0] != 'B' and s[-1] != 'A':
        continue

    if s[0] == 'B' and s[-1] == 'A':
        S_tail_a_head_b += 1
    elif s[0] == 'B':
        S_head_b += 1
    elif s[-1] == 'A':
        S_tail_a += 1

if S_tail_a_head_b == 0:
    count_ab += min(S_head_b, S_tail_a)
else:
    if sum([S_tail_a, S_head_b]) > 0:
        count_ab += S_tail_a_head_b + min(S_head_b, S_tail_a)
    else:
        count_ab += S_tail_a_head_b - 1

print(count_ab)
Buggy code:
N = int(input())

ans = 0
last_a_num = 0
first_b_num = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[-1] == 'A':
        last_a_num += 1
    if s[0] == 'B':
        first_b_num += 1

connect_num = min(last_a_num, first_b_num)
if last_a_num == first_b_num:
    connect_num -= 1
ans += connect_num

print(ans)

Fixed code:
N = int(input())

ans = 0
last_a_num = 0
first_b_num = 0
multi_ba_num = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[-1] == 'A':
        last_a_num += 1
    if s[0] == 'B':
        first_b_num += 1
    if s[-1] == 'A' and s[0] == 'B':
        multi_ba_num += 1

connect_num = min(last_a_num, first_b_num)
if last_a_num == first_b_num and last_a_num == multi_ba_num:
    if connect_num > 0:
        connect_num -= 1
ans += connect_num

print(ans)
Buggy code:
n = int(input())
s = []
for i in range(n):
	s.append(input())

ans = 0
for i in s:
	ans += i.count("AB")

# print(ans)

A = 0
B = 0
AB = 0

for i in s:
	if i[0] == 'B' and i[-1] == 'A':
		AB += 1
	elif i[0] == 'B':
		B += 1
	elif i[-1] == 'A':
		A += 1

if AB == 0:
	ans = min(A,B)
else:
	if A+B == 0:
		ans = AB-1
	else:
		ans = AB + min(A,B)

print(ans)

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

ans = 0
for i in s:
	ans += i.count("AB")

# print(ans)

A = 0
B = 0
AB = 0

for i in s:
	if i[0] == 'B' and i[-1] == 'A':
		AB += 1
	elif i[0] == 'B':
		B += 1
	elif i[-1] == 'A':
		A += 1

if AB == 0:
	ans += min(A,B)
else:
	ans += AB + min(A,B) + (-1 if (A+B) == 0 else 0)

print(ans)
Buggy code:
n = int(input())
sl = [input() for _ in range(n)]
sb,la,cnt,both = 0,0,0,0
for s in sl:
  cnt += s.count("AB")
  if s[0] == "B":
    sb += 1
  if s[-1] == "A":
    la += 1
  if s[0] + s[-1] == "BA":
    both += 1
if sb == both or la == n:
  cnt -= 1
print(cnt + min(la,sb))

Fixed code:
n = int(input())
sl = [input() for _ in range(n)]
sb,la,cnt,both = 0,0,0,0
for s in sl:
  cnt += s.count("AB")
  if s[0] + s[-1] == "BA":
    both += 1
  elif s[0] == "B":
    sb += 1
  elif s[-1] == "A":
    la += 1
if both:
  cnt += both-1
  if la:
    cnt += 1
    la -= 1
  if sb:
    cnt += 1
    sb -= 1
print(cnt + min(la,sb))
Buggy code:
n = int(input())
a,b,ab,ans = 0,0,0,0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A" and s[0] == "B":
        ab += 1
    elif s[0] == "B":
        b += 1
    elif s[-1] =="A":
        a += 1

print(ans+ab-1+min(min(a+1,b),min(a,b+1)))

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

N = int(input())

for i in range(N):
    s = input()
    ans += s.count("AB")
    if s[0] == "B":
        first_b += 1
    if s[-1] == "A":
        last_a += 1
    if s[0] == "B" and s[-1] == "A":
        same +=1

ans += min(first_b, last_a)
if same > 0 and first_b ==  last_a and first_b = same:
    ans -=1
 print(ans)

Fixed code:
ans = 0
first_b = 0
last_a = 0
same = 0

N = int(input())

for i in range(N):
    s = input()
    ans += s.count("AB")
    if s[0] == "B":
        first_b += 1
    if s[-1] == "A":
        last_a += 1
    if s[0] == "B" and s[-1] == "A":
        same +=1

ans += min(first_b, last_a)
if same > 0 and first_b ==  last_a and first_b == same:
    ans -=1
print(ans)
Buggy code:
import sys,math,collections,itertools
input = sys.stdin.readline

N = int(input())
Aend = []
Bstart = []
Ae_Bs = []
cnt = 0

for i in range(N):
    S = input().rstrip()
    if S[0]=='B' and S[-1] =='A':
        Ae_Bs.append(S)
    elif S[0] == 'B':
        print(S)
        Bstart.append(S)
    elif S[-1] == 'A':
        Aend.append(S)
    cnt += S.count('AB')

lAe_Bs = len(Ae_Bs)
lBstart = len(Bstart)
lAend = len(Aend)

if lAe_Bs >0:
    cnt += lAe_Bs -1
    
cnt += min(lBstart,lAend)
cnt += (abs(lBstart-lAend)>0 and lAe_Bs >0)

print(cnt)


Fixed code:
import sys,math,collections,itertools
input = sys.stdin.readline

N = int(input())
Aend = []
Bstart = []
Ae_Bs = []
cnt = 0

for i in range(N):
    S = input().rstrip()
    if S[0]=='B' and S[-1] =='A':
        Ae_Bs.append(S)
    elif S[0] == 'B':
        Bstart.append(S)
    elif S[-1] == 'A':
        Aend.append(S)
    cnt += S.count('AB')

lAe_Bs = len(Ae_Bs)
lBstart = len(Bstart)
lAend = len(Aend)

if lAe_Bs >0:
    cnt += lAe_Bs -1
    
if lAe_Bs > 0 and lAend >0:
    cnt += 1
    lAend -=1
if lAe_Bs > 0 and lBstart>0:
    cnt +=1
    lBstart -=1

cnt += min(lAend,lBstart)

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

fi = 0
sec = 0
thi = 0
forth = 0

for i in range(n):
    cnt =  s[i].count("AB")
    if cnt>=1:
        fi+=cnt

    syo = s[i][0]
    saigo = s[i][-1]

    if syo =="B" and saigo == "A":
        sec+=1
    elif syo == "B":
        thi+=1
    elif saigo=="A":
        forth+=1

su=0
if sec>=1:
    su += sec-1

su+=fi

if thi!=forth:
    su+=min(thi,forth)+1
else:
    su+=thi

print(fi,sec,thi,forth)
print(su)


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

fi = 0
sec = 0
thi = 0
forth = 0

for i in range(n):
    cnt =  s[i].count("AB")
    if cnt>=1:
        fi+=cnt

    syo = s[i][0]
    saigo = s[i][-1]

    if syo =="B" and saigo == "A":
        sec+=1
    elif syo == "B":
        thi+=1
    elif saigo=="A":
        forth+=1

su=0
if sec>=1:
    su += sec-1

su+=fi

if sec>=1:
    su+=min(1,thi)
    su+=min(1,forth)
    su+=min(thi-min(1,thi),forth-min(1,forth))
else:
    su+=min(thi,forth)

#print(fi,sec,thi,forth)
print(su)
Buggy code:
n = int(input())
s = []
ca = 0
cb = 0
cab = 0
for i in range(n):
    tmp = input()
    s.append(tmp)
    if tmp[0] == "B" and tmp[-1] == "A":
        cab += 1
    elif tmp[-1] == "A":
        ca += 1
    elif tmp[0] == "B":
        cb += 1
cnt = 0
for ss in s:
    cnt += ss.count("AB")
if ca == cb == 0 and cab > 0:
    cnt -= 1
print(cnt + min(ca, cb))


Fixed code:
n = int(input())
cnt = 0
ca = 0
cb = 0
cab = 0
for i in range(n):
    tmp = input()
    cnt += tmp.count("AB")
    if tmp[0] == "B" and tmp[-1] == "A":
        cab += 1
    elif tmp[0] == "B":
        cb += 1
    elif tmp[-1] == "A":
        ca += 1
if ca == cb == 0 and cab > 0:
    cnt -= 1
print(cnt + cab + min(ca, cb))
Buggy code:

N = int(input())
countA = 0
countB = 0
countC = 0
kcount = 0

for i in range(N):
    K = input()
    kcount += K.count("AB")
    K = list(K)
    if K[0] == "B" and K[-1] == "A":
        countC += 1 
    if K[-1] == "A":
        countA += 1
    if K[0] == "B"
        countB += 1
if countC>=1 and (countB-countC)==0 and (countA-countC)==0:
    print(kcount+countC-1)
elif countC >=1 and (countB-countC)==0 and (countA-countC)>=1:
    print(kcount+countC)
elif countC >=1 and (countB-countC)>=1 and (countA-countC)==0:
    print(kcount+countC)
elif countB > countA:
    print(kcount+countA)
else:
    print(kcount+countB)

Fixed code:

N = int(input())
countA = 0
countB = 0
countC = 0
kcount = 0

for i in range(N):
    K = input()
    kcount += K.count("AB")
    K = list(K)
    if K[0] == "B" and K[-1] == "A":
        countC += 1 
    if K[-1] == "A":
        countA += 1
    if K[0] == "B":
        countB += 1
if countC>=1 and (countB-countC)==0 and (countA-countC)==0:
    print(kcount+countC-1)
elif countC >=1 and (countB-countC)==0 and (countA-countC)>=1:
    print(kcount+countC)
elif countC >=1 and (countB-countC)>=1 and (countA-countC)==0:
    print(kcount+countC)
elif countB > countA:
    print(kcount+countA)
else:
    print(kcount+countB)
Buggy code:
N = int(input())
cnt = 0
endA = 0
startB = 0
both = 0
for i in range(N):
	s = input()
	flgA = s[-1] == "A"
	flgB = s[0] == "B"
	if flgA and flgB:
		both += 1
	elif flgA:
		endA += 1
	elif flgB:
		startB += 1
	cnt += s.count("AB")

cnt += both - 2
if endA > 0:
	cnt += 1
	endA -= 1
if startB > 0:
	cnt += 1
	startB -= 1
cnt += min(endA, startB)
print(cnt)


Fixed code:
N = int(input())
cnt = 0
endA = 0
startB = 0
both = 0
for i in range(N):
	s = input()
	flgA = s[-1] == "A"
	flgB = s[0] == "B"
	if flgA and flgB:
		both += 1
	elif flgA:
		endA += 1
	elif flgB:
		startB += 1
	cnt += s.count("AB")

if both > 0:
	if both > 1:
		cnt += both - 1
	if endA > 0:
		cnt += 1
		endA -= 1
	if startB > 0:
		cnt += 1
		startB -= 1
cnt += min(endA, startB)
print(cnt)
Buggy code:
n = int(input())
 
g1 = []
g2 = []
g3 = []
 
cnt = 0
 
for i in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        g3.append(s)
    elif s[0] == "B":
        g1.append(s)
    elif s[-1] == "A":
        g2.append(s)
        
a = len(g1)
b = len(g2)
c = len(g3)
 
if a == b:
    if a == c:
        cnt += c * 2
    elif a > c:
        cnt += c
        cnt += a
    elif a < c:
        if a == 0:
            cnt += c - 1
        else]:
            cnt += a
            cnt += c
elif a > b:
    cnt += b
    cnt += c
elif b > a:
    cnt += a
    cnt += c
 
print(cnt)

Fixed code:
n = int(input())
 
g1 = []
g2 = []
g3 = []
 
cnt = 0
 
for i in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        g3.append(s)
    elif s[0] == "B":
        g1.append(s)
    elif s[-1] == "A":
        g2.append(s)
        
a = len(g1)
b = len(g2)
c = len(g3)
 
if a == b:
    if a == c:
        cnt += c * 2
    elif a > c:
        cnt += c
        cnt += a
    elif a < c:
        if a == 0:
            cnt += c - 1
        else:
            cnt += a
            cnt += c
elif a > b:
    cnt += b
    cnt += c
elif b > a:
    cnt += a
    cnt += c
 
print(cnt)