Buggy python submission (verdict: Time Limit Exceeded):
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))+[3,3,3]
N=len(S)


if N<=2:
    print(0)
    sys.exit()

T=0
F=True
while F:
    j=0
    F=False
    while j<=N-3:
        k=j
        while (S[k]==0) and (S[k+1]==1) and (S[k+2]==2):
            k+=3

        for i in range(j,k):
            S[i]=(S[i]+1)%3
            F=True
            
        T+=(k-j)//3
        j=k+1
print(T)
            


Fixed submission:
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))
N=len(S)

if N<=2:
    print(0)
    sys.exit()

T=0
U=0

I=0
K=0
while I<N-2:
    if (S[I]==0) and (S[I+1]==1) and (S[I+2]==2):
        U+=1
        T+=U
        I+=3
    elif S[I]==0:
        U+=1
        I+=1
    elif (S[I]==1) and (S[I+1]==2):
        T+=U
        I+=2
    else:
        U=0
        I+=1

if (S[-2]==1 and S[-1]==2) and S[-3]!=0:
    T+=U
    
print(T)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))
N=len(S)

if N<=2:
    print(0)
    sys.exit()

T=0
F=True
while F:
    F=False
    for i in range(N-2):
        if (S[i]==0) and (S[i+1]==1) and (S[i+2]==2):
            T+=1
            S[i],S[i+1],S[i+2]=1,2,0
            F=F or True

print(T)
            


Fixed submission:
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))
N=len(S)

if N<=2:
    print(0)
    sys.exit()

T=0
U=0

I=0
K=0
while I<N-2:
    if (S[I]==0) and (S[I+1]==1) and (S[I+2]==2):
        U+=1
        T+=U
        I+=3
    elif S[I]==0:
        U+=1
        I+=1
    elif (S[I]==1) and (S[I+1]==2):
        T+=U
        I+=2
    else:
        U=0
        I+=1

if (S[-2]==1 and S[-1]==2) and S[-3]!=0:
    T+=U
    
print(T)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))
N=len(S)

if N<=2:
    print(0)
    sys.exit()

T=0
F=True
while F:
    F=False
    for i in range(N-2):
        if (S[i]==0) and (S[i+1]==1) and (S[i+2]==2):
            T+=1
            S[i],S[i+1],S[i+2]=1,2,0
            F=F or True

print(T)
            


Fixed submission:
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))
N=len(S)

if N<=2:
    print(0)
    sys.exit()

T=0
U=0

I=0
K=0
while I<N-2:
    if (S[I]==0) and (S[I+1]==1) and (S[I+2]==2):
        U+=1
        T+=U
        I+=3
    elif S[I]==0:
        U+=1
        I+=1
    elif (S[I]==1) and (S[I+1]==2):
        T+=U
        I+=2
    else:
        U=0
        I+=1

if (S[-2]==1 and S[-1]==2) and S[-3]!=0:
    T+=U
    
print(T)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))
N=len(S)

if N<=2:
    print(0)
    sys.exit()

T=0
F=True
while F:
    F=False
    for i in range(N-2):
        if (S[i]==0) and (S[i+1]==1) and (S[i+2]==2):
            T+=1
            S[i],S[i+1],S[i+2]=1,2,0
            F=F or True

print(T)
            


Fixed submission:
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))
N=len(S)

if N<=2:
    print(0)
    sys.exit()

T=0
U=0

I=0
K=0
while I<N-2:
    if (S[I]==0) and (S[I+1]==1) and (S[I+2]==2):
        U+=1
        T+=U
        I+=3
    elif S[I]==0:
        U+=1
        I+=1
    elif (S[I]==1) and (S[I+1]==2):
        T+=U
        I+=2
    else:
        U=0
        I+=1

if (S[-2]==1 and S[-1]==2) and S[-3]!=0:
    T+=U
    
print(T)