Buggy python submission (verdict: Time Limit Exceeded):
import copy
n = int(input())
s = input()
ans = 0
for i in range(n-2):
    RGB = ['R', 'G', 'B']
    RGB.remove(s[i])
    Rc = s[i+1:].count('R')
    Gc = s[i+1:].count('G')
    Bc = s[i+1:].count('B')
    # print(RGB, Rc, Gc, Bc)
    for j in range(i+1, n-1):
        RGBcopy = copy.copy(RGB)
        if not s[j] in RGB:
            if s[j] == 'R':
                Rc = max(0, Rc-1)
            elif s[j] == 'G':
                Gc = max(0, Gc-1)
            else:
                Bc = max(0, Bc-1)
            continue
        
        if s[j] == 'R':
            Rc = max(0, Rc-1)
            RGBcopy.remove('R')
            if RGBcopy[0] == 'G':
                cnt = Gc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'G':
                        cnt = max(0, cnt-1)
            else:
                cnt = Bc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'B':
                        cnt = max(0, cnt-1)

        elif s[j] == 'G':
            Gc = max(0, Gc-1)
            RGBcopy.remove('G')
            if RGBcopy[0] == 'R':
                cnt = Rc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'R':
                        cnt = max(0, cnt-1)
            else:
                cnt = Bc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'B':
                        cnt = max(0, cnt-1)

        else:
            Bc = max(0, Bc-1)
            RGBcopy.remove('B')
            if RGBcopy[0] == 'G':
                cnt = Gc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'G':
                        cnt = max(0, cnt-1)
            else:
                cnt = Rc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'R':
                        cnt = max(0, cnt-1)
        # print(i, j, cnt, Rc, Gc, Bc)
        ans += cnt
        # print(RGBcopy)

print(ans)


Fixed submission:
import copy
n = int(input())
s = input()
ans = 0
for i in range(n-2):
    RGB = ['R', 'G', 'B']
    RGB.remove(s[i])
    Rc = s[i+1:].count('R')
    Gc = s[i+1:].count('G')
    Bc = s[i+1:].count('B')
    # print(RGB, Rc, Gc, Bc)
    for j in range(i+1, n-1):
        RGBcopy = copy.copy(RGB)
        if not s[j] in RGB:
            if s[j] == 'R':
                Rc = max(0, Rc-1)
            elif s[j] == 'G':
                Gc = max(0, Gc-1)
            else:
                Bc = max(0, Bc-1)
            continue
        
        if s[j] == 'R':
            Rc = max(0, Rc-1)
            RGBcopy.remove('R')
            if RGBcopy[0] == 'G':
                cnt = Gc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'G':
                        cnt = max(0, cnt-1)
            else:
                cnt = Bc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'B':
                        cnt = max(0, cnt-1)

        elif s[j] == 'G':
            Gc = max(0, Gc-1)
            RGBcopy.remove('G')
            if RGBcopy[0] == 'R':
                cnt = Rc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'R':
                        cnt = max(0, cnt-1)
            else:
                cnt = Bc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'B':
                        cnt = max(0, cnt-1)

        else:
            Bc = max(0, Bc-1)
            RGBcopy.remove('B')
            if RGBcopy[0] == 'G':
                cnt = Gc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'G':
                        cnt = max(0, cnt-1)
            else:
                cnt = Rc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'R':
                        cnt = max(0, cnt-1)
        # print(i, j, cnt, Rc, Gc, Bc)
        ans += cnt
        # print(RGBcopy)

print(ans)