Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()

# r[i] : s[i:]に何個あるか
r = [0] * (n + 1)
g = [0] * (n + 1)
b = [0] * (n + 1)

ss = s[::-1]
for i in range(n):
    c = ss[i]
    # if i > 0:
    #     r[i] = r[i - 1]
    #     g[i] = g[i - 1]
    #     b[i] = b[i - 1]

    r[i + 1] = r[i] + (c == 'R')
    g[i + 1] = g[i] + (c == 'G')
    b[i + 1] = b[i] + (c == 'B')

    # if ss[i] == 'R':
    #     r[i] += 1
    # if ss[i] == 'G':
    #     g[i] += 1
    # if ss[i] == 'B':
    #     b[i] += 1

r = r[::-1]
g = g[::-1]
b = b[::-1]

ans = 0



# rgb
for i in range(n):
    if s[i] != 'R':
        continue
    for j in range(i + 1, n):
        if s[j] != 'G':
            continue
        d = j - i
        ans += b[j]
        if j + d < n and s[j + d] == 'B':
            ans -= 1
# rbg
for i in range(n):
    if s[i] != 'R':
        continue
    for j in range(i + 1, n):
        if s[j] != 'B':
            continue
        d = j - i
        ans += g[j]
        if j + d < n and s[j + d] == 'G':
            ans -= 1
# grb
for i in range(n):
    if s[i] != 'G':
        continue
    for j in range(i + 1, n):
        if s[j] != 'R':
            continue
        d = j - i
        ans += b[j]
        if j + d < n and s[j + d] == 'B':
            ans -= 1
# gbr
for i in range(n):
    if s[i] != 'G':
        continue
    for j in range(i + 1, n):
        if s[j] != 'B':
            continue
        d = j - i
        ans += r[j]
        if j + d < n and s[j + d] == 'R':
            ans -= 1
# brg
for i in range(n):
    if s[i] != 'B':
        continue
    for j in range(i + 1, n):
        if s[j] != 'R':
            continue
        d = j - i
        ans += g[j]
        if j + d < n and s[j + d] == 'G':
            ans -= 1
# bgr
for i in range(n):
    if s[i] != 'B':
        continue
    for j in range(i + 1, n):
        if s[j] != 'G':
            continue
        d = j - i
        ans += r[j]
        if j + d < n and s[j + d] == 'R':
            ans -= 1

print(ans)


Fixed submission:
n = int(input())
s = input()

# r[i] : s[i:]に何個あるか
r = [0] * (n + 1)
g = [0] * (n + 1)
b = [0] * (n + 1)

ss = s[::-1]
for i in range(n):
    c = ss[i]
    # if i > 0:
    #     r[i] = r[i - 1]
    #     g[i] = g[i - 1]
    #     b[i] = b[i - 1]

    r[i + 1] = r[i] + (c == 'R')
    g[i + 1] = g[i] + (c == 'G')
    b[i + 1] = b[i] + (c == 'B')

    # if ss[i] == 'R':
    #     r[i] += 1
    # if ss[i] == 'G':
    #     g[i] += 1
    # if ss[i] == 'B':
    #     b[i] += 1

r = r[::-1]
g = g[::-1]
b = b[::-1]

ans = 0



# rgb
for i in range(n):
    if s[i] != 'R':
        continue
    for j in range(i + 1, n):
        if s[j] != 'G':
            continue
        d = j - i
        ans += b[j]
        if j + d < n and s[j + d] == 'B':
            ans -= 1
# rbg
for i in range(n):
    if s[i] != 'R':
        continue
    for j in range(i + 1, n):
        if s[j] != 'B':
            continue
        d = j - i
        ans += g[j]
        if j + d < n and s[j + d] == 'G':
            ans -= 1
# grb
for i in range(n):
    if s[i] != 'G':
        continue
    for j in range(i + 1, n):
        if s[j] != 'R':
            continue
        d = j - i
        ans += b[j]
        if j + d < n and s[j + d] == 'B':
            ans -= 1
# gbr
for i in range(n):
    if s[i] != 'G':
        continue
    for j in range(i + 1, n):
        if s[j] != 'B':
            continue
        d = j - i
        ans += r[j]
        if j + d < n and s[j + d] == 'R':
            ans -= 1
# brg
for i in range(n):
    if s[i] != 'B':
        continue
    for j in range(i + 1, n):
        if s[j] != 'R':
            continue
        d = j - i
        ans += g[j]
        if j + d < n and s[j + d] == 'G':
            ans -= 1
# bgr
for i in range(n):
    if s[i] != 'B':
        continue
    for j in range(i + 1, n):
        if s[j] != 'G':
            continue
        d = j - i
        ans += r[j]
        if j + d < n and s[j + d] == 'R':
            ans -= 1

print(ans)