Problem:
 Score : 400 points    Problem Statement We have a string S of length N consisting of R , G , and B .  Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:   S_i \neq S_j , S_i \neq S_k , and S_j \neq S_k .  j - i \neq k - j .       Constraints  1 \leq N \leq 4000  S is a string of length N consisting of R , G , and B .         Input Input is given from Standard Input in the following format:  N  S       Output Print the number of triplets in question.        Sample Input 1 4
RRGB      Sample Output 1 1  Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.       Sample Input 2 39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB      Sample Output 2 1800

Solution (Python):
n=int(input())
S=input()
R=0
G=0
B=0
for s in S:
    if s=='R':
        R+=1
    elif s=='G':
        G+=1
    elif s=='B':
        B+=1
r=0
g=0
b=0
l=len(S)
mix=0
for i in range(l):
    for j in range(i,l):
        k = 2*j-i
        if k >= l:
            break
        
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            mix+=1
            
print(R*G*B-mix)

Solution (Python):
n = int(input())
s = input()

r = s.count("R")
g = s.count("G")
b = s.count("B")

count = r * g * b

for i in range(n-1):
    for j in range(i + 1, i + ((n - 1 - i) // 2) + 1):
        k = 2 * j - i
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            count -= 1

print(count)

Solution (Python):
N=int(input())
S=input()
R=S.count("R")
G=S.count("G")
B=S.count("B")


count=R*G*B
for i in range(0,N-2):
    for j in range(i+1,N-1):
        k=j+(j-i)
        if k>=N:
            break
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            count=count-1

print(count)

Solution (Python):

def main():
    import sys
    input = sys.stdin.readline
    
    
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    n=int(input())
    s=input().rstrip('\n')
    a=0
    b=0
    c=0
    for i in range(n):
        if s[i]=="R":
            a+=1
        elif s[i]=="G":
            b+=1
        elif s[i]=="B":
            c+=1
    num=a*b*c
    
    for i in range(n-2):
        for j in range(1,n//2+1):
            if i+2*j<n:
                if s[i]!=s[i+j] and s[i+j]!=s[i+2*j] and s[i]!=s[i+2*j]:
                    num-=1
    print(num)

if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=input()

cnt=s.count('R')*s.count('G')*s.count('B')
for i in range(n-2):
    for j in range(i+1,n-1):
        k=2*j-i
        if j<k<n:
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                cnt-=1
print(cnt)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False



def c(i, j, k):
    return j - i != k - j


def main():
    N = I()
    s = S()
    r, g, b = 0, 0, 0

    for i in range(N):
        if s[i] == 'R':
            r += 1
        elif s[i] == 'G':
            g += 1
        else:
            b += 1

    all_c = r * g * b
    sub = 0
    test = 0

    for i in range(N):
        for j in range(i+1, N):
            k = (j - i) + j
            if k >= N or s[i] == s[j] or s[k] == s[i] or s[j] == s[k]:
                continue
            
            sub += 1

    
    print(all_c - sub)


if __name__ == '__main__':
    main()

Solution (Python):
import collections
N = int(input())
S = list(input())
C = collections.Counter(S)
ans = C['R'] * C['G'] * C['B']
for i in range(1, N - 1):
    b = S[i]
    ans -= sum(
        a != b and b != c and c != a for a, c in zip(S[i - 1::-1], S[i + 1:]))
print(ans)

Solution (Python):
def main():
    n = int(input())
    s = input()

    sum_count = s.count('R') * s.count('G') * s.count('B')

    r_list = set()
    g_list = set()
    b_list = set()

    for i in range(n):
        if s[i] == 'R':
            r_list.add(i)
        elif s[i] == 'G':
            g_list.add(i)
        elif s[i] == 'B':
            b_list.add(i)

    for r in r_list:
        for g in g_list:
            L = g - r
            if g + L in b_list:
                sum_count -= 1
            if r - L in b_list:
                sum_count -= 1
            if L % 2 == 0 and L//2 + r in b_list:
                sum_count -= 1

    print(sum_count)

if __name__ == '__main__':
    main()

Solution (Python):












N = int(input())
S = input()

def resolve_v1(N, S):
    count = 0
    
    for k in range(N): 
        for j in range(k):
            for i in range(1, j): 
                if S[k] != S[j] and S[j] != S[i] and S[i] != S[k] and not k-j == j-i:
                    count += 1

    return count

def resolve_v2(N, S):
    count = 0

    for i in range(N):
        for j in range(i+1, N):
            if S[i] == S[j]:
                continue

            for k in range(j+1, N):
                if S[i] == S[k] or S[j] == S[k] or k-j == j-i:
                    continue

                count += 1

    return count






def resolve_v3(N, S):
    r, g, b = 0, 0, 0
    for i in range(N):
        if S[i] == 'R':
            r += 1
        elif S[i] == 'G':
            g += 1
        elif S[i] == 'B':
            b += 1

    count = r * g * b

    for i in range(N-2):
        for j in range(i+1, N-1):
            k = 2 * j - i

            if k >= N:
                break

            if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                count -= 1

    return count


if __name__ == '__main__':
    
    count = resolve_v3(N, S)
    print(count)

Solution (Python):
N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
  for j in range(i, N):
    k = 2*j - i
    if k >= N:
      break
    if len(set([S[i], S[j],S[k]])) ==3:
      ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()


R=[]
G=[]
B=[]
for n,s in enumerate(S):
    if s=="R":
        R.append(n)
    elif s=="G":
        G.append(n)
    else:
        B.append(n)

ans = len(R)*len(G)*len(B)

for i in range(1,N//2 + 1):
    for s in range(N -i*2):
        if S[s]!=S[s+i] and S[s+i]!=S[s+2*i] and S[s] != S[s+2*i]:
            ans -= 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
S = input()

cnt = defaultdict(int)
for i in range(N):
    s = S[i]
    cnt[s] += 1

ans = cnt["R"] * cnt["G"] * cnt["B"]

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if 2 * j - i < N:
            if S[i] != S[j] and S[i] != S[2 * j - i] and S[j] != S[2 * j - i]:
                ans -= 1
        else:
            break

print(ans)

Solution (Python):
def solve():
    N = int(input())
    S = list(input())
    ans = S.count('R') * S.count('G') * S.count('B')
    for i in range(N-2):
        for j in range(i+1, N + (-(N-i-1)//2)):
            k = j+(j-i)
            if len(set([S[i], S[j], S[k]])) == 3:
                ans-=1
    return ans
print(solve())

Solution (Python):
from collections import defaultdict
N = int(input())
S = input()

cnt = defaultdict(int)
for i in range(N):
    s = S[i]
    cnt[s] += 1

ans = cnt["R"] * cnt["G"] * cnt["B"]

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if 2 * j - i < N:
            if S[i] != S[j] and S[i] != S[2 * j - i] and S[j] != S[2 * j - i]:
                ans -= 1
        else:
            break

print(ans)

Solution (Python):
N = int(input())
S = list(input())
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N-2):
    for j in range(i+1, N + (-(N-i-1)//2)):
        k = j+(j-i)
        if len(set([S[i], S[j], S[k]])) == 3:
            ans-=1
print(ans)

Solution (Python):
N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N-2):
    for j in range(i+1, N + (-(N-i-1)//2)):
        k = j+(j-i)
        if len(set([S[i], S[j], S[k]])) == 3:
            ans-=1
print(ans)

Solution (Python):
n = int(input())
s = input()
r_count = s.count('R')
g_count = s.count('G')
b_count = s.count('B')

count=0
for i in range(n):
    a = s[i]
    for j in range(i+1,n):
        if (2*j-i)>(n-1):
            break
        b=s[j]
        c=s[2*j-i]
        if a!=b and b!=c and a!=c:
            count+=1
print(r_count*g_count*b_count-count)

Solution (Python):
n = int(input())
s = input()
r_count = s.count('R')
g_count = s.count('G')
b_count = s.count('B')

count=0
for i in range(n):
    a = s[i]
    for j in range(i+1,n):
        if (2*j-i)>(n-1):
            continue
        b=s[j]
        c=s[2*j-i]
        if a!=b and b!=c and a!=c:
            count+=1
print(r_count*g_count*b_count-count)

Solution (Python):
n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')

cnt = r*g*b

for i in range(n):
    j = 1
    while i+2*j < n:
        if s[i] != s[i+j] and s[i+j] != s[i+2*j] and s[i+2*j] != s[i]:
            cnt -= 1
            j += 1
        else:
            j += 1
print(cnt)

Solution (Python):
from collections import Counter
from sys import exit

n=int(input())
s=input()

l=list(Counter(s).values())
if len(l)<3:
	print("0")
	exit()
	
res=l[0]*l[1]*l[2]

for i in range(1,n//2+1):
	for j in range(n):
		if j+i*2>n-1:
			break
		a=j
		b=j+i
		c=b+i
		if s[a]!=s[b] and s[b]!=s[c] and s[c]!=s[a]:
			res-=1
			
print(res)

Solution (Python):
def solve2():
    n = int(input())
    s = list(input())

    r = s.count('R')
    g = s.count('G')
    b = s.count('B')

    cnt = 0
    for i in range(n):
        for j in range(i+1,n):
            k = j*2-i
            if k >= n:
                break
            elif s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt +=1
    return r*g*b - cnt

print(solve2())

Solution (Python):
n = int(input())
s = list(input())

r = s.count('R')
g = s.count('G')
b = s.count('B')

cnt = 0
for i in range(n):
    for j in range(i+1,n):
        k = j*2-i
        if k >= n:
            break
        elif s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            cnt +=1
print(r*g*b - cnt)

Solution (Python):
if __name__ == "__main__":
    N = int(input())
    S = input()

    
    

    R = 0
    G = 0
    B = 0
    for s in S:
        if s == "R":
            R += 1
        elif s == "G":
            G += 1
        else:
            B += 1    
    total = R*G*B

    
    for i in range(N-2):
        for j in range(i+1, N-1):
            
            k = j + (j-i)
            
            
            
            if k >= N or S[i] == S[j] or S[i] == S[k] or S[j] == S[k]:
                continue
            
            total -= 1
    
    print(total)

Solution (Python):


N = int(input())
S = str(input())
R_cnt = S.count('R')
G_cnt = S.count('G')
B_cnt = S.count('B')
total = R_cnt * G_cnt * B_cnt

cnt = 0
for i in range(N):
    for j in range(i + 1, N):
        k = j + (j - i)
        if k < N and (S[i] != S[j] and S[j] != S[k] and S[k] != S[i]):
            cnt += 1

print(total - cnt)

Solution (Python):
n = int(input())
s = list(input())
ans = (s.count("R")*s.count("G")*s.count("B"))

for i in range(n):
    for j in range(n-2*i):
        if {s[j], s[j+i], s[j+2*i]} == {"R", "G", "B"}:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = list(input())
def main():
    ans = s.count('R') * s.count('G') * s.count('B')

    if(n<4):
        return 0
    
    for i in range(n):
        for j in range(i+1,n):
            k= 2*j-i
            if k>=n:
                continue
            if(s[i]==s[j] or s[i]== s[k] or s[j]==s[k]):
                continue
            ans -= 1
    return ans
print(main())

Solution (Python):
N = int(input())
S = input()
 
r = S.count('R')
g = S.count('G')
b = S.count('B')
 
counter = 0
for i in range(N):
    for j in range(i+1, N):
        k = 2 *j - i
        if k >= N:
            break
        elif S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            counter += 1
 
print(r * g * b - counter)

Solution (Python):


from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

import copy
N = int(input())
S = input()










        



















c = Counter(S)
al = c["R"] * c["G"] * c["B"]

for i in range(N):
    for j in range(i+1, N):
        k = 2*j - i
        if k >= N: break
        if S[i] != S[k] and S[i] != S[j] and S[j] != S[k]:
            al -= 1
print(al)

Solution (Python):



import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf



import math

import bisect

class Solver():

    R = 'R'
    G = 'G'
    B = 'B'

    def __init__(self, n, s):
        self.n = n
        self.s = s
        self.rgb = self.make_rgb()
        self.large = self.calc_large()
        
        
        self.trident = 0

    def make_rgb(self):
        rgb = {
                self.R: [],
                self.G: [],
                self.B: [],
                }
        for i, c in enumerate(self.s):
            rgb[c].append(i)
        return rgb

    def calc_large(self):
        r = self.rgb[self.R]
        g = self.rgb[self.G]
        b = self.rgb[self.B]
        l = [
                (self.R, len(r)),
                (self.G, len(g)),
                (self.B, len(b)),
                ]
        l = sorted(l, key=lambda tpl: tpl[1])
        
        
        return l[-1][0]

    def get_smalls(self):
        if self.large == self.R:
            return self.rgb[self.G], self.rgb[self.B]
        if self.large == self.G:
            return self.rgb[self.R], self.rgb[self.B]
        if self.large == self.B:
            return self.rgb[self.G], self.rgb[self.R]

    def run(self):
        s1, s2 = self.get_smalls()
        for i1 in s1:
            for i2 in s2:
                self.check_3way(i1, i2)
        return self.calc_ans()

    def check_3way(self, i1, i2):
        if not i1 < i2:
            i1, i2 = i2, i1
        d = i2 - i1
        self.hit_then_count(i1 - d)
        self.hit_then_count(i1 + d / 2)
        self.hit_then_count(i2 + d)

    def hit_then_count(self, key):
        if int(key) != key:
            return
        if not (0 <= key < len(self.s)):
            return
        key = int(key)
        if self.s[key] == self.large:
            self.trident += 1

    def calc_ans(self):
        r = len(self.rgb[self.R])
        g = len(self.rgb[self.G])
        b = len(self.rgb[self.B])
        return r * g * b - self.trident




if __name__ == '__main__':
    n = int(input())
    s = input()
    ans = Solver(n, s).run()
    
    print(ans)

Solution (Python):
N = int(input())
List = list(input())
ans = (List.count("R"))*(List.count("G"))*(List.count("B"))

for i in range(1,2000):
  for j in range(N-i*2):
    if {List[j], List[j+i], List[j+2*i]} == {"R","G","B"}:
      ans -=1
print(ans)

Solution (Python):
n = int(input())
s = input()

r = len([i for i in range(n) if s[i] == "R"])
g = len([i for i in range(n) if s[i] == "G"])
b = len([i for i in range(n) if s[i] == "B"])

count = 0
for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k >= n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            count += 1

print(r * g * b - count)

Solution (Python):
n=int(input())
S=input()
R={}
G={}
B={}
r=0
g=0
b=0
for i in range(n):
    if S[i] == 'R':
        R[i] = None
    elif S[i] == 'G':
        G[i] = None
    else:
        B[i] = None
cnt=len(R)*len(G)*len(B)
for b in B:
    for g in G:
        if (b+g)/2 in R:
            cnt-=1
for b in B:
    for r in R:
        if (b+r)/2 in G:
            cnt-=1
for r in R:
    for g in G:
        if (r+g)/2 in B:
            cnt-=1
print(cnt)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
S = list(input())
 
r = S.count("R")
g = S.count("G")
b = S.count("B")
 
 
A = list(range(len(S)))
cnt = 0
for x, y in combinations(A, 2):
	if (x+y)//2 == (x+y)/2:
		z = (x+y)//2
		if S[x] != S[y] and S[x] != S[z] and S[y] != S[z]:
			cnt += 1
 
print(r*g*b-cnt)

Solution (Python):
from collections import Counter
from itertools import combinations, product
n = int(input())
s = input()
ct = {i : [] for i in "RGB"}
for i, _s in enumerate(s):
    ct[_s].append(i)

cnt = 1
for v in ct.values():
    cnt *= len(v)

for combi in product(ct["R"], ct["G"]):
    _min = min(combi) - abs(combi[0] - combi[1])
    _max = max(combi) + abs(combi[0] - combi[1])
    if _min >= 0 and s[_min] == "B":
        cnt -= 1
    if _max < n and s[_max] == "B":
        cnt -= 1
    if (combi[1] - combi[0]) % 2 == 0 and s[abs(combi[0] - combi[1]) // 2 + min(combi)] == "B":
        cnt -= 1
print(cnt)

Solution (Python):
n=int(input())
s=input()

r,g,b=s.count('R'),s.count('G'),s.count('B')

cnt=0

for i in range(n):
  d=0
  while 1:
    d+=1
    j=i+d
    k=j+d
    if not k<n:
      break
    if not (s[i]==s[j] or s[j]==s[k] or s[k]==s[i]):
      cnt+=1
print(r*g*b-cnt)

Solution (Python):
n = int(input())
s = input()
ans = s.count("R")*s.count("G")*s.count("B")

for j in range(1, n-1):
    for i in range(j):
        k = 2*j - i
        if k<n and s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()

r = s.count("R")
g = s.count("G")
b = s.count("B")
rgb = r*g*b

count = 0
for i in range(n):
  for j in range(i+1, n-1):
    k = 2*j-i
    if k >= n or s[i] == s[j] or s[j] == s[k] or s[i] == s[k]:
      continue
    rgb -= 1
print(rgb)

Solution (Python):
N = int(input())
S = input()

r = 0
g = 0
b = 0
for i, s in enumerate(S):
    if s == 'R':
        r += 1
    elif s == 'G':
        g += 1
    else:
        b += 1

ans = r * g * b
for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == 'R':
            if S[j] == 'G':
                if 2 * j - i < N and S[j + j - i] == 'B':
                    ans -= 1
            if S[j] == 'B':
                if 2 * j - i < N and S[j + j - i] == 'G':
                    ans -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                if 2 * j - i < N and S[j + j - i] == 'B':
                    ans -= 1
            if S[j] == 'B':
                if 2 * j - i < N and S[j + j - i] == 'R':
                    ans -= 1
        else:
            if S[j] == 'R':
                if 2 * j - i < N and S[j + j - i] == 'G':
                    ans -= 1
            if S[j] == 'G':
                if 2 * j - i < N and S[j + j - i] == 'R':
                    ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()

r = 0
g = 0
b = 0
cnt_S = []
for i, s in enumerate(S):
    if s == 'R':
        r += 1
    elif s == 'G':
        g += 1
    else:
        b += 1
    cnt_S.append([r, g, b])

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == 'R':
            if S[j] == 'G':
                ans += cnt_S[-1][2] - cnt_S[j][2]
                if 2 * j - i < N and S[j + j - i] == 'B':
                    ans -= 1
            if S[j] == 'B':
                ans += cnt_S[-1][1] - cnt_S[j][1]
                if 2 * j - i < N and S[j + j - i] == 'G':
                    ans -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                ans += cnt_S[-1][2] - cnt_S[j][2]
                if 2 * j - i < N and S[j + j - i] == 'B':
                    ans -= 1
            if S[j] == 'B':
                ans += cnt_S[-1][0] - cnt_S[j][0]
                if 2 * j - i < N and S[j + j - i] == 'R':
                    ans -= 1
        else:
            if S[j] == 'R':
                ans += cnt_S[-1][1] - cnt_S[j][1]
                if 2 * j - i < N and S[j + j - i] == 'G':
                    ans -= 1
            if S[j] == 'G':
                ans += cnt_S[-1][0] - cnt_S[j][0]
                if 2 * j - i < N and S[j + j - i] == 'R':
                    ans -= 1
print(ans)

Solution (Python):

"""
Created on Wed May  6 02:13:01 2020

@author: shinba
"""
import sys

N = int(input())
S = input()

R = []
G = []
B = []

for i in range(N):
    if S[i] == "R":
        R.append(i+1)
    elif S[i] == "G":
        G.append(i+1)
    else:
        B.append(i+1)

if R==[] or G==[] or B==[]:
    print(0)
    sys.exit()
    
ans = len(R)*len(G)*len(B)

m = min(B)
M = max(B)

for i in R:
    for j in G:
        a = 2*j - i 
        b = (i+j)//2 if (i+j)%2 == 0 else 0
        c = 2*i - j
        if m <= a <= M:
            if S[a-1] == "B":
                ans -= 1
        if b != 0 and S[b-1] == "B":
            ans -= 1
        if m <= c <= M:
            if S[c-1] == "B":
                ans -= 1
            
print(ans)

Solution (Python):
n = int(input())
S = input()

R = {}
G = {}
B = {}
rc = 0
gc = 0
bc = 0

for i in range(n):
    index = i * 2
    if S[i] == 'R':
        R[index] = i
        rc += 1
    elif S[i] == 'G':
        G[index] = i
        gc += 1
    else:
        B[index] = i
        bc += 1

cnt = rc * gc * bc

for b in B:
    for g in G:
        if (B[b] + G[g]) in R:
            cnt -= 1

for r in R:
    for b in B:
        if (R[r] + B[b]) in G:
            cnt -= 1

for r in R:
    for g in G:
        if (R[r] + G[g]) in B:
            cnt -= 1

print(cnt)

Solution (Python):
n = int(input())
S = input()

R = {}
G = {}
B = {}

for i in range(n):
    if S[i] == 'R':
        R[i] = None
    elif S[i] == 'G':
        G[i] = None
    else:
        B[i] = None



cnt = 0

cnt = len(R) * len(G) * len(B)

for b in B:
    for g in G:
        if (b + g) / 2 in R:
            cnt -= 1

for r in R:
    for b in B:
        if (r + b) / 2 in G:
            cnt -= 1

for r in R:
    for g in G:
        if (r + g) / 2 in B:
            cnt -= 1
            
print(cnt)

Solution (Python):
N = int(input())
S = input()
counter = 0

for i in range(N-2):
    for j in range(i+1,(N+i-1)//2+1):
        k = 2*j-i
        if not S[i]==S[j] and  not S[j]==S[k] and not S[i]==S[k]:
            counter += 1

rgb = S.count("R")*S.count("G")*S.count("B")
print(rgb-counter)

Solution (Python):
a=int(input())
s=input()
ans=s.count("R")*s.count("G")*s.count("B")
for i in range(a):
  for j in range(i+1,(a+i+1)//2):
    if s[i]!=s[j] and s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
      ans-=1
print(ans)

Solution (Python):
a=int(input())
s=input()
ans=s.count("R")*s.count("G")*s.count("B")
for i in range(a):
  for j in range(i+1,(a+i+1)//2):
    if s[i]!=s[j] and s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
      ans-=1
print(ans)

Solution (Python):
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
for i in range(n):
    for j in range(i+1,n):
        k = j - i + j
        if k<n and s[k]!=s[i] and s[k]!=s[j] and s[i]!=s[j]:
            ans-=1
print(ans)

Solution (Python):
n = int(input())
s = str(input())

r = s.count('R')
g = s.count('G')
b = s.count('B')
all_count = r*g*b

reduce = 0
for i in range(1, n-2+1):
    for j in range(i+1, (n+i)//2+1):
        k = 2*j -i
        if k > n: continue
        if s[i-1] != s[j-1] and s[i-1] != s[k-1] and s[j-1] != s[k-1]:
            reduce += 1
print(all_count - reduce)

Solution (Python):
N = int(input())
S = list(input())

ans = S.count("R") * S.count("G") * S.count("B")

for i in range(N):
    for j in range(i+1,N):
        k = j-i+j
        if k<N and S[k]!=S[i] and S[k]!=S[j] and S[i]!=S[j]:
            ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()

ans = s.count('R')*s.count('G')*s.count('B')  

for i in range(0, n - 2):
    for j in range(i + 1,  n - 1):
        k = (j - i) + j
        if k >= n:
            continue
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = list(input())
A = S.count("R") * S.count("G") * S.count("B")

for i in range(N):
  for j in range(i+1, N):
    k = j-i+j
    if k<N and S[k] != S[j] and S[k] != S[i] and S[i] !=S[j]:
      A -= 1    

print(A)

Solution (Python):
N = int(input())
S = input()
A = S.count("R") * S.count("G") * S.count("B")

for i in range(N):
  for j in range(i+1, N):
    k = j-i+j
    if k<N and S[k] != S[j] and S[k] != S[i] and S[i] !=S[j]:
      A -= 1    

print(A)

Solution (Python):
n = int(input())
s = input()
rr = 0
gg = 0
bb = 0
for i in range(n):
    if s[i] == "R":
        rr += 1
    if s[i] == "G":
        gg += 1
    if s[i] == "B":
        bb += 1

ans = rr * gg * bb

for j in range(n):
    for i in range(min(j+1, n-j)):
        if s[j - i] != s[j] and s[j] != s[j + i] and s[j + i] != s[j - i]:
            ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()

r = 0; g = 0; b = 0
for i in range(N):
    if S[i] == "R":
        r+=1
    elif S[i] == "G":
        g+=1
    else:
        b+=1

all = r*g*b; sub = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue
        else:
            k = 2 * j - i
            if k >= N:
                continue
            else:
                if (S[j] == S[k]) or (S[i] == S[k]):
                    continue
                else:
                    sub+=1

print(all - sub)

Solution (Python):
N=int(input())
S=input()
r,g,b = S.count('R'),S.count('G'),S.count('B')
total = r*g*b

count = 0
for d in range(1,N):
  for i in range(N-2*d):
    if S[i]!=S[i+d] and S[i+d]!=S[i+2*d] and S[i+2*d] != S[i]:
      count+=1

print(total-count)

Solution (Python):
import itertools
N = int(input())
S = input()
out = S.count('R') * S.count('G') * S.count('B')

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))
for i in range(1, N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      out -= 1
print(out)

Solution (Python):
N = int(input())
S = input()

dic = {'R':0, 'G':0, 'B':0}
for i in range(N):
  dic[S[i]] += 1

ans = dic['R'] * dic['G'] * dic['B']

for i in range(N):
  for j in range(i+1, N):
    if 2*j - i < len(S):
      if S[i] == 'R' and S[j] == 'G' and S[2*j - i] == 'B':
        ans -= 1

      if S[i] == 'R' and S[j] == 'B' and S[2*j - i] == 'G':
        ans -= 1

      if S[i] == 'G' and S[j] == 'R' and S[2*j - i] == 'B':
        ans -= 1

      if S[i] == 'G' and S[j] == 'B' and S[2*j - i] == 'R':
        ans -= 1
      
      if S[i] == 'B' and S[j] == 'G' and S[2*j - i] == 'R':
        ans -= 1

      if S[i] == 'B' and S[j] == 'R' and S[2*j - i] == 'G':
        ans -= 1

print(ans)

Solution (Python):
from collections import Counter
n=int(input())
s=list(input())
C=Counter(s)
r,g,b=C["R"],C["G"],C["B"]
ans=r*g*b

for i in range(n-2):
    for j in range(i+1,n-1):
        k=2*j-i
        if k<=n-1 and s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                ans-=1

print(ans)

Solution (Python):
n = int(input())
s = input()

r, g, b = 0, 0, 0
rr, gg, bb = [0] * n, [0] * n, [0] * n
rcs, gcs, bcs = [0] * n, [0] * n, [0] * n
for i, c in enumerate(s):
    if c == 'R':
        r = r + 1
        rr[i] = 1
    elif c == 'G':
        g = g + 1
        gg[i] = 1
    else:
        b = b + 1
        bb[i] = 1
    rcs[i], gcs[i], bcs[i] = r, g, b

ans = 0
for j in range(1, n-1):
    l = min(j, n-1-j)
    if s[j] == 'R':
        cs1, cs2, s1, s2 = gcs, bcs, gg, bb
    elif s[j] == 'G':
        cs1, cs2, s1, s2 = rcs, bcs, rr, bb
    else:
        cs1, cs2, s1, s2 = rcs, gcs, rr, gg
    c1 = cs1[j-1], cs1[n-1]-cs1[j]
    c2 = cs2[j-1], cs2[n-1]-cs2[j]
    ans = ans + c1[1] * c2[0] + c1[0] * c2[1]
    for i in range(1, l+1):
        ans = ans - s1[j-i] * s2[j+i] - s1[j+i] * s2[j-i]

print(ans)

Solution (Python):
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i)
	elif s[i] == "G":
		g.append(i)
	else:
		b.append(i)
count = 0
unchi = set(b)
for i in r:
	for j in g:
		u = abs(i-j)
		count += [0,1][(max(i,j)+u) in unchi] + [0,1][(min(i,j)-u) in unchi] + [0,1][u%2 == 0 and (i+j)//2 in unchi]
print(len(r)*len(g)*len(b)-count)

Solution (Python):
n = int(input())
s = input()

r=[]
g=[]
b=[]

for i in range(n):
    if s[i]=='R':
        r.append(i)
    if s[i]=='G':
        g.append(i)
    if s[i]=='B':
        b.append(i)

ans=len(r)*len(g)*len(b)

for i in range(n):
    for j in range(i+1,n):
        k=2*j-i
        if k<n and s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            ans -= 1

print(ans)

Solution (Python):
N = int(input())
S = list(input())
R = S.count('R')
G = S.count('G')
B = S.count('B')
ans = R*G*B
for i in range(N):
    for k in range(i+1,N):
        if (k+i)%2 == 1:
            continue
        else:
            j = (k+i)//2
            if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
num = r*g*b
for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >=n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            num -=1
print(num)

Solution (Python):
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
num = r*g*b
for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >=n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            num -=1
print(num)

Solution (Python):
import collections

n = int(input())
s = input()

c = collections.Counter(s)
r = c['R'] * c['G'] * c['B']

for i in range(n):
    for k in range(i, n):
        j = (i + k) // 2
        if (i + k) % 2 == 0 and 0 < 2 < n:
            if s[i] != s[k] and s[k] != s[j] and s[i] != s[j]:
                r -= 1

print(r)

Solution (Python):
n = int(input())
s = list(input())
ans = s.count('R')*s.count('G')*s.count('B')
for i in range(n):
    if s[i] == 'R':
        s[i] = 1
    elif s[i] == 'G':
        s[i] = 2
    elif s[i] == 'B':
        s[i] = 4

for i in range(n):
    for j in range(i+1,n):
        k = 2*j -i
        if k >= n:
            continue
        elif s[i]+s[j]+s[k] == 7:
            ans -= 1

print(ans)