Buggy python submission (verdict: Time Limit Exceeded):
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    N = int(input())
    s = input()

    counter = Counter(s)
    rs = counter['R']
    gs = counter['G']
    bs = counter['B']
    

    rcnt, gcnt, bcnt = 0, 0, 0
    rl, gl, bl = list(), list(), list()

    for i in range(N):
        if s[i] == 'R':
            rcnt += 1
        elif s[i] == 'G':
            gcnt += 1
        elif s[i] == 'B':
            bcnt += 1
        
        rl.append(rs-rcnt)
        gl.append(gs-gcnt)
        bl.append(bs-bcnt)
    ans = 0
    
    for i in range(N-2):
        if s[i] != 'R':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'G':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'B'
                ok = int(ok)
                ans += bl[j] - ok
            else:
                ans += bl[j]

    
    #RBG
    for i in range(N-2):
        if s[i] != 'R':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'B':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'G'
                ok = int(ok)
                ans += gl[j] - ok
            else:
                ans += gl[j]
    
    for i in range(N-2):
        if s[i] != 'B':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'R':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'G'
                ok = int(ok)
                ans += gl[j] - ok
            else:
                ans += gl[j]
    
    for i in range(N-2):
        if s[i] != 'B':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'G':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'R'
                ok = int(ok)
                ans += rl[j] - ok
            else:
                ans += rl[j]
    for i in range(N-2):
        if s[i] != 'G':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'R':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'B'
                ok = int(ok)
                ans += bl[j] - ok
            else:
                ans += bl[j]
    for i in range(N-2):
        if s[i] != 'G':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'B':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'R'
                ok = int(ok)
                ans += rl[j] - ok
            else:
                ans += rl[j]



    print(ans)
    return

if __name__ == '__main__':
    main()

Fixed submission:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    N = int(input())
    s = input()

    counter = Counter(s)
    rs = counter['R']
    gs = counter['G']
    bs = counter['B']
    

    rcnt, gcnt, bcnt = 0, 0, 0
    rl, gl, bl = list(), list(), list()

    for i in range(N):
        if s[i] == 'R':
            rcnt += 1
        elif s[i] == 'G':
            gcnt += 1
        elif s[i] == 'B':
            bcnt += 1
        
        rl.append(rs-rcnt)
        gl.append(gs-gcnt)
        bl.append(bs-bcnt)
    ans = 0
    
    for i in range(N-2):
        if s[i] != 'R':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'G':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'B'
                ok = int(ok)
                ans += bl[j] - ok
            else:
                ans += bl[j]

    
    #RBG
    for i in range(N-2):
        if s[i] != 'R':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'B':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'G'
                ok = int(ok)
                ans += gl[j] - ok
            else:
                ans += gl[j]
    
    for i in range(N-2):
        if s[i] != 'B':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'R':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'G'
                ok = int(ok)
                ans += gl[j] - ok
            else:
                ans += gl[j]
    
    for i in range(N-2):
        if s[i] != 'B':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'G':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'R'
                ok = int(ok)
                ans += rl[j] - ok
            else:
                ans += rl[j]
    for i in range(N-2):
        if s[i] != 'G':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'R':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'B'
                ok = int(ok)
                ans += bl[j] - ok
            else:
                ans += bl[j]
    for i in range(N-2):
        if s[i] != 'G':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'B':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'R'
                ok = int(ok)
                ans += rl[j] - ok
            else:
                ans += rl[j]



    print(ans)
    return

if __name__ == '__main__':
    main()