Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
from copy import copy
  
def main():
    n = int(input())
    s = input()
    RGB = ['R', 'G', 'B']
    
    rgb_cnt = [0,0,0]
    for i in range(n-1, -1, -1):
        if s[i] == 'R':
            rgb_cnt[0] += 1
        if s[i] == 'G':
            rgb_cnt[1] += 1
        if s[i] == 'B':
            rgb_cnt[2] += 1
            
    sumv = 0
    rgb_cnt_cp = copy(rgb_cnt)
    for i in range(n-2):
        
        c1 = s[i]
        rgb = ['R', 'G', 'B']
        rgb_idx = rgb.index(c1)
        
        rgb_cnt_cp[rgb_idx] -= 1
        rgb_cnt_cp2 = copy(rgb_cnt_cp)
        
        #print(rgb_cnt_cp2)
        
        for j in range(i+1, n):
            c2 = s[j]
            if c2 == c1:
                continue
            rgb_idx2 = rgb.index(c2)
            rgb_cnt_cp2[rgb_idx2] -= 1
            #print(rgb_idx2)
            
            rgb2 = copy(RGB)
            rgb2.pop(rgb2.index(c1))
            rgb2.pop(rgb2.index(c2))
            c3 = rgb2[0]
            
            c3idx = RGB.index(c3)
            #print(rgb_cnt_cp2[c3idx])
            sumv += rgb_cnt_cp2[c3idx]
            
            
        #print(rgb_cnt_cp2)
        
    for i in range(n-1):
        c1 = s[i]
        rgb = ['R', 'G', 'B']
        rgb_idx = rgb.index(c1)
        
        rgb.pop(rgb.index(c1))
        
        for j in range(i+1, n):
            c2 = s[j]
            if c2 == c1:
                continue
            rgb2 = copy(rgb)
            rgb2.pop(rgb.index(c2))
            c3 = rgb2[0]
            
            k = j + (j-i)
            
            if k > n - 1:
                break
            if c3 == s[k]:
                #print(i,j,k)
                sumv -= 1
            
        
    print(sumv)
    #print(rgb_cnt)
        
  
if __name__ == '__main__':
    main()

Fixed submission:
#coding:utf-8
from copy import copy
  
def main():
    n = int(input())
    s = input()
    RGB = ['R', 'G', 'B']
    
    rgb_cnt = [0,0,0]
    for i in range(n-1, -1, -1):
        if s[i] == 'R':
            rgb_cnt[0] += 1
        if s[i] == 'G':
            rgb_cnt[1] += 1
        if s[i] == 'B':
            rgb_cnt[2] += 1
            
    sumv = 0
    rgb_cnt_cp = copy(rgb_cnt)
    for i in range(n-2):
        
        c1 = s[i]
        rgb = ['R', 'G', 'B']
        rgb_idx = rgb.index(c1)
        
        rgb_cnt_cp[rgb_idx] -= 1
        rgb_cnt_cp2 = copy(rgb_cnt_cp)
        
        #print(rgb_cnt_cp2)
        
        for j in range(i+1, n):
            c2 = s[j]
            if c2 == c1:
                continue
            rgb_idx2 = rgb.index(c2)
            rgb_cnt_cp2[rgb_idx2] -= 1
            #print(rgb_idx2)
            
            rgb2 = copy(RGB)
            rgb2.pop(rgb2.index(c1))
            rgb2.pop(rgb2.index(c2))
            c3 = rgb2[0]
            
            c3idx = RGB.index(c3)
            #print(rgb_cnt_cp2[c3idx])
            sumv += rgb_cnt_cp2[c3idx]
            
            
        #print(rgb_cnt_cp2)
        
    for i in range(n-1):
        c1 = s[i]
        rgb = ['R', 'G', 'B']
        rgb_idx = rgb.index(c1)
        
        rgb.pop(rgb.index(c1))
        
        for j in range(i+1, n):
            c2 = s[j]
            if c2 == c1:
                continue
            rgb2 = copy(rgb)
            rgb2.pop(rgb.index(c2))
            c3 = rgb2[0]
            
            k = j + (j-i)
            
            if k > n - 1:
                break
            if c3 == s[k]:
                #print(i,j,k)
                sumv -= 1
            
        
    print(sumv)
    #print(rgb_cnt)
        
  
if __name__ == '__main__':
    main()