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()
rs = [0]*n
gs = [0]*n
bs = [0]*n
for i in reversed(range(n)):
    if s[i] == 'R':
        rs[i] += 1
    elif s[i] == 'G':
        gs[i] += 1
    else:
        bs[i] += 1
for i in reversed(range(n-1)):
    rs[i] += rs[i+1]
    gs[i] += gs[i+1]
    bs[i] += bs[i+1]
res = 0
for i in range(n):
    for j in range(i+1,n-1):
        if s[i] == s[j]:
            continue
        if s[i]!='B' and s[j]!='B':
            res += bs[j+1]
            if j-i+j < n:
                if s[j-i+j] == 'B':
                    res -=1
        elif s[i]!='G' and s[j]!='G':
            res += gs[j+1]
            if j - i + j < n:
                if s[j-i+j] == 'G':
                    res -=1
        else:
            res += rs[j+1]
            if j - i + j < n:
                if s[j-i+j] == 'R':
                    res -= 1
print(res)

Solution (Python):
from operator import mul
from functools import reduce

def combination(n, r):
    if not 0 <= r <= n: return 0
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

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

R = dict()
L = dict()


R["R"] = S.count("R")
L["R"] = 0
R["G"] = S.count("G")
L["G"] = 0
R["B"] = S.count("B")
L["B"] = 0

def num(n, x):
    res = 0
    for i in range(1,min(n,N-1-n) + 1):
        if S[n-i] != x and S[n+i] != x and S[n-i] != S[n+i]:
            res += 1
    return res

R[S[0]] -= 1
L[S[0]] += 1
res = 0
for i in range(1,N-1):
    x = S[i]
    R[x] -= 1
    three = sum( R[x]*L[x]for x in ["R","G","B"])
    res += three + ((N-1-i)-R[x])*L[x] + R[x]*(i-L[x]) + num(i,x)
    L[x] += 1

print(combination(N,3) - res)

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

RGBlist = [[S.count('R')],[S.count('G')],[S.count('B')]]


for i in range(1,N):
    RGBlist[0].append(RGBlist[0][-1] - (S[i-1] == 'R'))
    RGBlist[1].append(RGBlist[1][-1] - (S[i-1] == 'G'))
    RGBlist[2].append(RGBlist[2][-1] - (S[i-1] == 'B'))

for i in range(N-2):
    si = S[i]
    
    for j in range(i+1,N-1):
        sj = S[j]
        if si==sj:
            continue
        else:
            RGB = set(['R','G','B'])
            RGB -= set([si,sj])
            k=j+1
            kji=j-i
            if RGB == {'R'}:
                rgb = 0
            elif RGB =={'G'}:
                rgb = 1
            else:
                rgb = 2
            if k+kji-1 < len(S):
                count += RGBlist[rgb][k] - ({S[k+kji-1]} == RGB)
            else:
                count += RGBlist[rgb][k]
            
print(count)

Solution (Python):
n=int(input())
s=input()
ans=0
cnt={}
cnt["R"]=0
cnt["G"]=0
cnt["B"]=0
for i in s:
    cnt[i]+=1
ans = cnt["R"] * cnt["G"] * cnt["B"]
for i in range(n-2):
    for gap in range(1,(n-i+1)//2):
        if s[i] != s[i+gap] and s[i+2*gap] != s[i+gap] and s[i+2*gap] != s[i]:
            ans-=1
print(ans)

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

if N<4:
    print(0)
else:
    a = [0] * N
    R, G, B = 0, 0, 0
    for i in range(N):
        if S[i] == "R":
            a[i] = 1
            R += 1
        if S[i] == "G":
            a[i] = 2
            G += 1
        if S[i] == "B":
            a[i] = 4
            B += 1

    rgb = R * G * B
    cnt = 0
    for i in range(N-2):
        for d in range(1, (N-i-1) // 2+1):
            if a[i] + a[i+d] + a[i+2*d] == 7:
                cnt += 1

    print(rgb -cnt)

Solution (Python):
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('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:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1
print(ans)

Solution (Python):

from collections import defaultdict


def main():
    """
    character  A ---- B ---- C
    index     X-dx    X     X+dx
        -> A_index + C_index == 2 * B_index
    """
    _, S = open(0).read().split()
    indices = defaultdict(set)
    for i, c in enumerate(S):
        indices[c].add(i)

    cnt = len(indices["R"]) * len(indices["G"]) * len(indices["B"])
    for x, y, z in ("RGB", "RBG", "BGR"):
        cnt -= sum(
            (i + j) % 2 == 0 and (i + j) // 2 in indices[z]
            for i in indices[x]
            for j in indices[y]
        )
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict


def main():
    _, S = open(0).read().split()
    indices, doubled_indices = defaultdict(list), defaultdict(set)
    for i, c in enumerate(S):
        indices[c].append(i)
        doubled_indices[c].add(2 * i)

    cnt = len(indices["R"]) * len(indices["G"]) * len(indices["B"])
    for x, y, z in ("RGB", "RBG", "BGR"):
        cnt -= sum(i + j in doubled_indices[z] for i in indices[x] for j in indices[y])
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):

"""
Created on Sat Jul 11 17:23:36 2020

@author: Aruto Hosaka
"""

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


color = ['R','G','B']
k = 1

A = {color[0]:0,color[1]:0,color[2]:0}

for c1 in color:
    for c2 in color:
        for c3 in color:
            if c1 != c2 and c2 != c3 and c1 != c3:
                A[(c1,c2,c3)] = 0

for c1 in color:
    for c2 in color:
        if c1!=c2:
            A[(c1,c2)] = 0

for i in range(N):
    if S[i] == 'R':
        A[('G','B','R')] += A[('G','B')]
        A[('B','G','R')] += A[('B','G')]
        A[('B','R')] += A['B']
        A[('G','R')] += A['G']
    if S[i] == 'G':
        A[('R','B','G')] += A[('R','B')]
        A[('B','R','G')] += A[('B','R')]
        A[('B','G')] += A['B']
        A[('R','G')] += A['R']
    if S[i] == 'B':
        A[('G','R','B')] += A[('G','R')]
        A[('R','G','B')] += A[('R','G')]
        A[('R','B')] += A['R']
        A[('G','B')] += A['G']
    A[S[i]] += 1

counter = 0
for c1 in color:
    for c2 in color:
        for c3 in color:
            if c1 != c2 and c2 != c3 and c1 != c3:
                counter += A[(c1,c2,c3)]
                
dc = 0
for i in range(N-2):
    k = 1
    while k*2+i < N:
        if S[i+k] != S[i] and S[i] != S[i+k*2] and S[i+k] != S[i+k*2]:
            dc += 1
        k += 1
print(counter-dc)

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

Solution (Python):
from collections import Counter
N = int(input())
S = list(input())
cnt = Counter(S)
ans = 1
for s in list('RGB'):
    ans *= cnt[s]

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]) & (S[j] != S[K]) & (S[i] != S[K]):
            ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()
R = set()
G = set()
B = set()
for i, s in enumerate(S):
    if s == 'R':
        R.add(i)
    elif s == 'G':
        G.add(i)
    elif s == 'B':
        B.add(i)

ans = 0
for r in R:
    for g in G:
        ans += len(B)
        d = abs(r-g)
        if (r+g) % 2 == 0 and (r+g)//2 in B:
            ans -= 1
        if r+d in B or g+d in B:
            ans -= 1
        if r-d in B or g-d in B:
            ans -= 1
print(ans)

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

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

ans = r*g*b
count  = 0
sub1 = 0

for i in range(n):
    for j in range(i+1,n):
        if 2*j-i >= n or 2*j-i <0:
            break
        elif c[2*j-i] != c[i] and c[i] != c[j] and c[2*j-i] != c[j]:
                sub1 += 1


print(ans-sub1)

Solution (Python):
from collections import defaultdict
 
 
def main():
    _, S = open(0).read().split()
    indices, doubled_indices = defaultdict(list), defaultdict(set)
    for i, c in enumerate(S):
        indices[c].append(i)
        doubled_indices[c].add(2 * i)
    cnt = S.count("R") * S.count("G") * S.count("B")
    for x, y, z in ["RGB", "GBR", "BRG"]:
        cnt -= sum(i + j in doubled_indices[z] for i in indices[x] for j in indices[y])
    print(cnt)
 
 
if __name__ == "__main__":
    main()

Solution (Python):
def main():
    import re
    n = int(input())
    s = input()
    rindex = set()
    gindex = set()
    bindex = set()
    for i in range(n):
        if s[i] == "R":
            rindex.add(i)
        elif s[i] == "G":
            gindex.add(i)
        else:
            bindex.add(i)
    ans = len(rindex) * len(gindex) * len(bindex)

    for r in rindex:
        for g in gindex:
            if r > g:
                gap = r - g
                b1 = r + gap
                b2 = g - gap
                b3 = g + (gap/2)

            elif r < g:
                gap = g - r
                b1 = g + gap
                b2 = r - gap
                b3 = r + (gap/2)
            if b1 in bindex:
                ans -= 1
            if b2 in bindex:
                ans -= 1  
            if b3 in bindex:
                ans -= 1  
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n = I()
    s = list(input())
    r = s.count('R')
    g = s.count('G')
    b = s.count('B')
    cnt = r*g*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[j] and s[i] != s[k] and s[j] != s[k]:
                cnt -= 1
    print(cnt)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
l = [1] * N
i = 0
d = {1:0,2:0,3:0}
for s in input():
    n = 1
    if s == "G":
        n = 2
    elif s == "B":
        n = 3
    l[i] = n
    d[n] += 1
    i += 1
cnt = d[1] * d[2] * d[3]
for j in range(1,(N - 1) // 2 + 1):
    for k in range(N - (2 * j)):
        if l[k] * l[k+j] * l[k+j+j] == 6:
            cnt -= 1
print(cnt)

Solution (Python):
from collections import Counter

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

counter = Counter(S)

r = 1
for c in "RGB":
    r *= counter[c]


for i in range(N):
    for j in range(i+1, N):
        k = 2*j-i
        
        if k < N and len({S[i], S[j], S[k]}) == 3:
            r -= 1

print(r)

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    n= int(input())
    s = str(input().rstrip())
    r= s.count('R')
    g=s.count('G')
    b = s.count('B')
    ans = 0
    for i in range(n-2):
        for j in range(i+1, (n+i+1)//2):
            k = 2*j-i
            t = {s[i], s[j], s[k]}
            if len(t) ==3:
                ans +=1
    print(r*g*b-ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
from operator import mul
from functools import reduce
from collections import Counter

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

c = Counter(s)
if len(c) != 3:
    print(0)
    sys.exit()

ans = reduce(mul, c.values())

for i in range(n - 1):
    x, y, z = i, i + 1, i + 2
    while z <= n - 1:
        if s[x] != s[y] and s[x] != s[z] and s[y] != s[z]:
            ans -= 1
        y += 1
        z += 2

print(ans)

Solution (Python):
from collections import Counter


n = int(input())
s = input()
count = Counter(s)

ans = count["R"] * count["G"] * count["B"]
for i in range(n):
    for j in range(i+1, n):
        k = 2*j - i
        if k > n - 1:
            break
        if s[i] != s[j] != s[k] != s[i]:
            ans -= 1

print(ans)

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

l = list(S)
ans = l.count('R')*l.count('G')*l.count('B')
for i in range(N-2):
  a = l[i]
  for j in range(1, (N+1-i)//2):
    b, c = l[i+j], l[i+2*j]
    if (a!=b) and (b!=c) and (c!=a):
      ans -= 1
        
print(ans)

Solution (Python):
from collections import Counter


def main():
    n = int(input())
    s = input()
    cnt = Counter(s)
    ans = cnt["R"] * cnt["G"] * cnt["B"]
    for gap in range(1, (n - 1) // 2 + 1):
        for i in range(n - gap * 2):
            if set([s[i], s[i + gap], s[i + 2 * gap]]) == {"R", "G", "B"}:
                ans -= 1
    print(ans)


if __name__ == "__main__":
    main()

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

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

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

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

su = s.count('R') * s.count('G') * s.count('B')
for i in range(n):
    for j in range(i+1,n):
        if s[i] != s[j]:
            for lis in ['R','G','B']:
                if lis != s[i] and lis != s[j]:
                    try:
                        su -= s[2*j-i].count(lis)
                    except:
                        pass

print(su)

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

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


cntR = cntG = cntB = 0
for i in range(N):
    if S[i] == 'R':
        cntR += 1
    elif S[i] == 'G':
        cntG += 1
    else:
        cntB += 1

ans = cntR*cntG*cntB - count
print(ans)

Solution (Python):
from collections import Counter
n=int(input())
s=input()
c=Counter(s)
ans = c['R'] * c['G'] * c['B']
for i in range(n-2):
	for j in range(i+1,n-1):
		k=j+(j-i)
		if k>=n:
			break
		if s[i]!=s[j]!=s[k]!=s[i]:
			ans -=1
print(ans)

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


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


for i in range(n):
    for d in range(1,n):
        j = i+d
        k = j+d
        if k > n-1:
            break
        if s[i]!=s[j]!=s[k]!=s[i]:
            tot -= 1

print(tot)

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


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


for i in range(n):
    for d in range(1,n):
        j = i+d
        k = j+d
        if k > n-1:
            break
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            tot -= 1

print(tot)

Solution (Python):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
S = readline().decode().rstrip()
R_cnt = S.count('R')
G_cnt = S.count('G')
B_cnt = S.count('B')
ans1 = R_cnt * G_cnt * B_cnt
cnt  = 0
d_max = N // 2 +1
for d in range(1,d_max):
    for i in range(N):
        if i+2*d >= N:
            break
        if (S[i] != S[i+d]) and (S[i+d] != S[i+2*d]) and (S[i] !=S[i+2*d]):
           cnt += 1
ans = ans1 - cnt
print(ans)

Solution (Python):
import collections
n=int(input())
S=input()
c=collections.Counter(S)
ans=c['R']*c['B']*c['G']
hiku=0
for i in range(n):
    for d in range(n):
        j=i+d
        k=j+d
        if k>n-1:
            break
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
          hiku+=1
print(ans-hiku)

Solution (Python):
import sys
input = sys.stdin.readline
import bisect





def main():
    n = int(input())
    s = input()
    R, G, B = [], [], []
    for i in range(n):
        index = i+1
        if s[i] == "R":
            R.append(index)
        elif s[i] == "G":
            G.append(index)
        elif s[i] == "B":
            B.append(index)
    all_count = len(R)*len(G)*len(B)
    count = 0
    
    
    for i in range(n):
        for j in range(i+1, n):
            if s[i] == s[j]:
                continue
            k = 2*j - i
            if k >= n:
                continue
            if s[k] == s[i] or s[k] == s[j]:
                continue
            count += 1
    print(all_count - count)

def bi(num, a, b, x):
    print((a * num) + (b * len(str(b))))
    if (a * num) + (b * len(str(b))) <= x:
        return False
    return True

def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Solution (Python):



from math import *
from copy import *					
from heapq import *					
from string import *				
from random import *				
from bisect import *				
from sys import stdin				
from sys import maxsize				
from operator import *				
from itertools import *				
from decimal import Decimal 		
from collections import Counter		
from collections import defaultdict 

'''
4
RGRBG

[0,2]
[1,4]
[3,5]
'''

def solve(s):
	n = len(s)
	r,g,b = s.count('R'),s.count('G'),s.count('B')
	sub = 0
	for i in range(n):
		for j in range(i+1,n):
			k = 2*j - i
			if(k >= n):
				break
			if(len(set([s[i],s[j],s[k]])) == 3):
				sub += 1
	return r*g*b - sub


N = int(stdin.readline())
l = input()
ans = solve(l)
print(ans)

Solution (Python):



from math import *
from copy import *					
from heapq import *					
from string import *				
from random import *				
from bisect import *				
from sys import stdin				
from sys import maxsize				
from operator import *				
from itertools import *				
from decimal import Decimal 		
from collections import Counter		
from collections import defaultdict 

'''
4
RGRBG

[0,2]
[1,4]
[3,5]
'''

def solve(s):
	n = len(s)
	r,g,b = s.count('R'),s.count('G'),s.count('B')
	sub = 0
	for i in range(n):
		for j in range(i+1,n):
			k = 2*j - i
			if(k >= n):
				break
			if(len(set([s[i],s[j],s[k]])) == 3):
				sub += 1
	return r*g*b - sub


N = int(stdin.readline())
l = input()
ans = solve(l)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')







def solve():
    N = II()
    S = LS()

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

    len_r = len(R)
    len_g = len(G)
    len_b = len(B)

    def g(lst, r,  j):
        l = -1
        while r - l > 1:
            m = (l + r) // 2
            if lst[m] <= j:
                l = m
            else:
                r = m
        return r


    def f(lst, r, out):
        l = -1
        while r - l > 1:
            m = (r + l) // 2
            if out < lst[m]:
                r = m
            elif out > lst[m]:
                l = m
            else:
                return True
        return False

    ans = 0
    for i in range(N):
        si = S[i]
        for j in range(i+1, N):
            sj = S[j]
            if si == sj: continue
            out = j + j - i
            
            if si == 'R':
                if sj == 'G':
                    
                    k = g(B, len_b, j)
                    
                    ans += len_b - k
                    if f(B, len_b, out):
                        ans -= 1
                elif sj == 'B':
                    
                    k = g(G, len_g, j)
                    ans += len_g - k
                    if f(G, len_g, out):
                        ans -= 1
            elif si == 'G':
                if sj == 'R':
                    
                    k = g(B, len_b, j)
                    ans += len_b - k
                    if f(B, len_b, out):
                        ans -= 1
                elif sj == 'B':
                    
                    k = g(R, len_r, j)
                    ans += len_r - k
                    if f(R, len_r, out):
                        ans -= 1
            elif si == 'B':
                if sj == 'R':
                    
                    k = g(G, len_g, j)
                    ans += len_g - k
                    if f(G, len_g, out):
                        ans -= 1
                elif sj == 'G':
                    
                    k = g(R, len_r, j)
                    ans += len_r - k
                    if f(R, len_r, out):
                        ans -= 1
    print(ans)




if __name__ == '__main__':
    solve()

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

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

ans = color_cnt['R']*color_cnt['G']*color_cnt['B']
for i in range(N):
    for diff in range(1, (N-i-1)//2 +1):
        ans -= len({S[i], S[i+diff], S[i+2*diff]}) == 3

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from numba import njit

MOD = 10**9 + 7

N = int(readline())
S = np.array(list(read().rstrip()), np.int8)

R = np.sum(S == ord('R'))
B = np.sum(S == ord('B'))
G = np.sum(S == ord('G'))
 
@njit
def f(S):
    N = len(S)
    ret = 0
    for i in range(N):
        for j in range(i + 1, N):
          k = j + j - i
          if k >= N:
            break
          if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ret += 1
    return ret
  
answer = R * B * G - f(S)
print(answer)

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

total = R*G*B

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

print(total)

Solution (Python):
import itertools

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

RGB = {"R":[0]*N, "G":[0]*N, "B":[0]*N}
for i in range(N):
    if S[i] == "R":
        RGB["R"][i] = 1
    elif S[i] == "G":
        RGB["G"][i] = 1
    else:
        RGB["B"][i] = 1

for k in RGB.keys():
    RGB[k] = list(itertools.accumulate(RGB[k]))

ans = 0
for i in range(N-2):
    l_i = S[i]
    for j in range(i+1, N-1):
        l_j = S[j]
        d = {"R", "G", "B"}
        if l_i != l_j:
            d.remove(l_i)
            d.remove(l_j)
            l_k = d.pop()
            ans += RGB[l_k][N-1] - RGB[l_k][j]
            if j*2 - i <= N-1:
                if S[j*2 - i] == l_k:
                    ans -= 1

print(ans)

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

for i in range(N-2):
    if S[i] == "R":
        num_G = S[i+1:].count('G')
        num_B = S[i+1:].count('B')
        total += num_G * num_B
    elif S[i] == "G":
        num_B = S[i+1:].count('B')
        num_R = S[i+1:].count('R')
        total += num_B * num_R
    elif S[i] == "B":
        num_G = S[i+1:].count('G')
        num_R = S[i+1:].count('R')
        total += num_G * num_R

for i in range(N-2):
    for j in range(i+1, N-1):
        if 2*j-i >= N:
            continue
        if S[j] != S[i] and S[2*j-i] != S[j] and S[2*j-i] != S[i]:
            total -= 1

print(total)

Solution (Python):



import collections

n=int(input())
S=input()
s=collections.Counter(S)
ans=s['R']*s['G']*s['B']
for i in range(n):
    for d in range(n):
        j=i+d
        k=j+d
        if k>n-1:
            break
        if 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 = S.count("R")
g = S.count("G")
b = S.count("B")
ans = 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 :
            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 = 'X' + input()

cnt_R, cnt_G, cnt_B = 0, 0, 0
for s in S[1:]:
    if s == 'R':
        cnt_R += 1
    elif s == 'G':
        cnt_G += 1
    elif s == 'B':
        cnt_B += 1

ans = cnt_R * cnt_G * cnt_B

for i in range(1,N-1):
    for j in range(i+1,N):
        k = 2*j - i
        if k > N:
            break
        
        a = S[i]; b = S[j]; c = S[k]
        if a != b and b != c and c != a:
            ans -= 1

print(ans)

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

ans = r*g*b
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]):
            ans -=1
print(ans)

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

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

ans=r*g*b
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]):

            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




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

Solution (Python):
N = int(input())
S = list(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

allcnt = r * g * b

sub = 0
for i in range(N):
    for j in range(i+1,N):
        if S[i] == S[j]:
            None
        else:
            k = 2*j-i
            if k >= N or S[i] == S[k] or S[j] == S[k]:
                None
            else:
                sub += 1

print(allcnt - sub)

Solution (Python):
from collections import Counter

n = int(input())
s = input()
cnt = Counter(s)

ans = cnt['R'] * cnt['G'] * cnt['B']
for i in range(n):
    for j in range(n):
        k = 2 * j - i
        if k > n-1:
            break
        if i < j < k and s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
            ans -= 1

print(ans)

Solution (Python):
from itertools import groupby
from statistics import median
from sys import exit

N = int(input())

line = ([(index, v) for index, v in enumerate(input())])


def types(line): return line[1]
def index(line): return line[0]


gp = groupby(sorted(line, key=types), types)

d = {}

for k, v in gp:
    indexes = set([i for i, value in v])
    d[k] = indexes

if 'R' not in d or 'G' not in d or 'B' not in d:
    print(0)
    exit()

delete = 0


for differ in range(1, N):
    for start in range(N):
        if start + differ*2 > N - 1:
            break

        i = line[start]
        j = line[start + differ]
        k = line[start + differ*2]

        if len({i[1], j[1], k[1]}) != 3:
            continue

        upper = max(i[0], j[0], k[0])
        lower = min(i[0], j[0], k[0])
        median_num = median([i[0], j[0], k[0]])
        if upper - median_num == median_num - lower:
            delete += 1

print(len(d['R']) * len(d['G']) * len(d['B']) - delete)

Solution (Python):
N = int(input())
S = input()
count_R = 0
count_G = 0
count_B = 0
count = 0
for i in range(N):
    if S[i] == 'R':
        count_R = count_R+1
    elif S[i] == 'G':
        count_G = count_G+1
    else:
        count_B = count_B+1
count = count_R*count_G*count_B

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

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

cum = [[0]*(N+1) for _ in range(3)]
for i in range(N):
    cum[0][i+1] = cum[0][i] + (S[i]=='R')
    cum[1][i+1] = cum[1][i] + (S[i]=='G')
    cum[2][i+1] = cum[2][i] + (S[i]=='B')
    
def color(i,j):
    RGB = [0]*3
    if S[i]=='R' or S[j]=='R':
        RGB[0] = 1
    if S[i]=='G' or S[j]=='G':
        RGB[1] = 1
    if S[i]=='B' or S[j]=='B':
        RGB[2] = 1
    
    if RGB[0]==0:
        return 0
    elif RGB[1]==0:
        return 1
    else:
        return 2
def col(s):
    if s=='R':
        return 0
    elif s=='G':
        return 1
    else:
        return 2

ans = 0
for i in range(N):
    for j in range(i+1,N):
        if S[i]!=S[j]:
            idx = color(i,j)
            ans += cum[idx][N]-cum[idx][j+1]
            k = 2*j-i
            if k<N and col(S[k])==idx:
                ans -= 1
print(ans)

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

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

ans = r * g * b

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

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

r = [0 for _ in range(N + 1)]
g = [0 for _ in range(N + 1)]
b = [0 for _ in range(N + 1)]

for i in reversed(range(N)):
    r[i] = r[i + 1]
    g[i] = g[i + 1]
    b[i] = b[i + 1]
    if S[i] == "R":
        r[i] += 1
    if S[i] == "G":
        g[i] += 1
    if S[i] == "B":
        b[i] += 1
        
ans = 0

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