Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(str(input()))
from collections import Counter

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            continue
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)

Fixed submission:
N = int(input())
S = list(str(input()))
from collections import Counter

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            break
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(str(input()))
from collections import Counter

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        c_lis = ['R', 'G', 'B']
        c_lis.remove(S[i])
        c_lis.remove(S[j])
        chara = c_lis[0]
        try:
            if S[i+(j-i)*2]==chara:
                ans-=1
        except:
            pass
print(ans)

Fixed submission:
N = int(input())
S = list(str(input()))
from collections import Counter

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            break
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(str(input()))
from collections import Counter

ans = 0
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        c = Counter(S[j:])
        c_lis = ['R', 'G', 'B']
        c_lis.remove(S[i])
        c_lis.remove(S[j])
        chara = c_lis[0]
        ans += c[chara]
        try:
            if S[i+(j-i)*2]==chara:
                ans-=1
        except:
            pass
print(ans)

Fixed submission:
N = int(input())
S = list(str(input()))
from collections import Counter

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            break
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(str(input()))
from collections import Counter

def solve(idx, lis, dis):
    if len(lis)==1:
        i = lis[0]
        c = Counter(S[idx:])
        if len(S)>dis:
            if S[dis]==i:
                return c[i]-1
            else:
                return c[i]
        else:
            return c[i]
    elif len(lis)==2:
        ans = 0
        p, q = lis
        s = S[idx:]
        for i, j in enumerate(s):
            if j==p:
                ans += solve(i+idx, (q,), i*2+idx)
            elif j==q:
                ans += solve(i+idx, (p,), i*2+idx)
        return ans
    else:
        ans = 0
        p, q, r = lis
        for i, j in enumerate(S):
            if j==p:
                ans += solve(i, (q, r), i)
            elif j==q:
                ans += solve(i, (p, r), i) 
            elif j==r:
                ans += solve(i, (p, q), i) 
        return ans

print(solve(0, ('R', 'G', 'B'), 0))

Fixed submission:
N = int(input())
S = list(str(input()))
from collections import Counter

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            break
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(str(input()))
from collections import Counter

def solve(idx, lis, dis):
    if len(lis)==1:
        i = lis[0]
        c = Counter(S[idx:])
        if len(S)>dis:
            if S[dis]==i:
                return c[i]-1
            else:
                return c[i]
        else:
            return c[i]
    elif len(lis)==2:
        ans = 0
        p, q = lis
        s = S[idx:]
        for i, j in enumerate(s):
            if j==p:
                ans += solve(i+idx, (q,), i*2+idx)
            elif j==q:
                ans += solve(i+idx, (p,), i*2+idx)
        return ans
    else:
        ans = 0
        p, q, r = lis
        for i, j in enumerate(S):
            if j==p:
                ans += solve(i, (q, r), i)
            elif j==q:
                ans += solve(i, (p, r), i) 
            elif j==r:
                ans += solve(i, (p, q), i) 
        return ans

print(solve(0, ('R', 'G', 'B'), 0))

Fixed submission:
N = int(input())
S = list(str(input()))
from collections import Counter

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            break
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(str(input()))
from collections import Counter
from functools import lru_cache

@lru_cache(maxsize=1000)
def solve(idx, lis, dis):
    if len(lis)==1:
        i = lis[0]
        c = Counter(S[idx:])
        if len(S)>dis:
            if S[dis]==i:
                return c[i]-1
            else:
                return c[i]
        else:
            return c[i]
    elif len(lis)==2:
        ans = 0
        p, q = lis
        for i, j in enumerate(S[idx:]):
            if j==p:
                ans += solve(i+idx, (q,), i*2+idx)
            elif j==q:
                ans += solve(i+idx, (p,), i*2+idx)
        return ans
    else:
        ans = 0
        p, q, r = lis
        for i, j in enumerate(S):
            if j==p:
                ans += solve(i, (q, r), i)
            elif j==q:
                ans += solve(i, (p, r), i) 
            elif j==r:
                ans += solve(i, (p, q), i) 
        return ans

print(solve(0, ('R', 'G', 'B'), 0))

Fixed submission:
N = int(input())
S = list(str(input()))
from collections import Counter

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            break
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(str(input()))
from collections import Counter

def solve(s, lis, dis):
    if len(lis)==1:
        i = lis[0]
        c = Counter(s)
        if len(s)>dis:
            if s[dis]==i:
                return c[i]-1
            else:
                return c[i]
        else:
            return c[i]
    elif len(lis)==2:
        ans = 0
        p, q = lis
        for i, j in enumerate(s):
            if j==p:
                ans += solve(s[i:], [q], i)
            elif j==q:
                ans += solve(s[i:], [p], i)
        return ans
    else:
        ans = 0
        p, q, r = lis
        for i, j in enumerate(s):
            if j==p:
                ans += solve(s[i:], [q, r], i)
            elif j==q:
                ans += solve(s[i:], [p, r], i) 
            elif j==r:
                ans += solve(s[i:], [p, q], i) 
        return ans

print(solve(S, ('R', 'G', 'B'), 0))

Fixed submission:
N = int(input())
S = list(str(input()))
from collections import Counter

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            break
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)