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())

ans=0
s=list(str(input()))
r=[]
b=[]
g=[]
for i in range(n):
    if s[i]=='R':
        r.append(i)
    if s[i]=='B':
        b.append(i)
    if s[i]=='G':
        g.append(i)
count=0
aida=1

while aida*2-1<=n:
    for i in range(n-aida*2):
        
       
        aa=set()
        aa.add(s[i])
        aa.add(s[i+aida])
        aa.add(s[i+aida*2])
        
        if len(aa)==3:
            count+=1
    aida+=1


print(len(r)*len(b)*len(g)-count)

Solution (Python):
from collections import deque

N = int(input())
S = input()
rgb = {"R":0, "G":0, "B":0}
RGBs = 0
for s in S:
    rgb[s] += 1

for i in range(N):
    tail = (N - 1 - i) // 2
    for d in range(1, tail + 1):
        Si = S[i]
        Sj = S[i + d]
        Sk = S[i + d * 2]
        if Si == Sj or Sj == Sk or Sk == Si:
            continue
        RGBs += 1

ans = rgb["R"] * rgb["G"] * rgb["B"] - RGBs
print(ans)

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

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

ans=r*g*b

for j in range(1,N-1):
    for d in range(1,min(j,N-j-1)+1):
        if S[j-d]!= S[j] and S[j]!= S[j+d] and S[j-d]!= S[j+d]:
            ans-=1
print(ans)

Solution (Python):
import sys
IS = lambda: sys.stdin.readline().rstrip()
II = lambda: int(IS())
MII = lambda: list(map(int, IS().split()))

def main():
    n = II()
    s = IS()
    cnt = 0
    rgb = {'R': [], 'G': [], 'B': []}
    for i in range(n):
        rgb[s[i]].append(i)
    dd = 0
    for i in range(n-1):
        for k in range(i+2, n):
            if (i+k)%2==0:
                j = (i+k)//2
                if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                    dd += 1
    all = len(rgb['R']) * len(rgb['G']) * len(rgb['B'])
    ans = all - dd
    print(ans)

if __name__ == '__main__':
    main()

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

bad_list = ['RGB', 'RBG', 'GRB', 'GBR', 'BRG', 'BGR']
RGB = {'R': 0, 'G': 0, 'B': 0}
other_map = {'R': 'GB', 'G': 'RB', 'B': 'RG'}
res = 0
for (i, s) in enumerate(S):
  other = other_map[s]
  res += RGB[other[0]] * RGB[other[1]]
  
  for j in range(((i+1)//2)+1)[1:]:
    if S[i::-j][:3] in bad_list:
      res -= 1

  RGB[s] += 1


print(res)

Solution (Python):
from collections import Counter
from math import ceil
n = int(input())
s = input()

if n <= 2:
    ans = 0
else:
    c = list(Counter(s).values())
    if len(c) <= 2:
        ans = 0
    else:
        ans = 1
        for i in c:
            ans *= i
            
        for i in range(ceil(n/2)-1):
            for j in range(n-(i+1)*2):
                if s[j] != s[j+i+1] and s[j] != s[j+(i+1)*2] and s[j+(i+1)*2] != s[j+i+1]:
                    ans -= 1
            
print(ans)

Solution (Python):
import collections
import functools
import operator



n=int(input())
s=input()
c=collections.Counter(s)
if len(c)<=2:
    print(0)
    exit()

l = functools.reduce(operator.mul, c.values())
for i in range(0,n-1):
    for j in range(i+1,n):
        k=j-i+j
        if k>=n:
            continue
        if s[i]!=s[j] and s[k]!=s[j] and s[i]!=s[k]:
            l-=1
print(l)

Solution (Python):
n = int(input())
s = input()
r = []
g = []
b = []
for i, item in enumerate(s):
    if item=="R":
        r.append(i)
    elif item=="G":
        g.append(i)
    else:
        b.append(i)
if (not r) or (not g) or (not b):
    print(0)
else:
    count = 0
    for low,mid,high in [(r,g,b), (r,b,g), (g,r,b), (g,b,r), (b,r,g), (b,g,r)]:
        for m in mid:
            ind = len(high) - 1
            for l in low:
                if l >= m:
                    continue
                while (l+high[ind] > m*2) and (ind>0):
                    ind -= 1
                if m < high[ind]:
                    if l+high[ind] == m*2:
                        count += 1
    print(len(r)*len(g)*len(b) - count)

Solution (Python):
n=int(input())
s=list(input())
b=[]
b_num,r_num,g_num=0,0,0
r=[]
g=[]
for i in range(n):
    if s[i]=="R":
        r_num+=1
    elif s[i]=="B":
        b_num+=1
    else:
        g_num+=1
    b.append(b_num)
    g.append(g_num)
    r.append(r_num)
def c(al,j):
    global r,g,b,r_num,g_num,b_num
    if al=="R":
        return r_num-r[j]
    elif al=="B":
        return b_num-b[j]
    else:
        return g_num-g[j]
ans=0
for i in range(n-1):
    for j in range(i+1,n):
        if j<=i:
            continue
        if s[i]==s[j]:
            continue
        a=[]
        a.append(s[i])
        a.append(s[j])
        
        sa=j-i
        if n-1<j+sa:
            if not "B" in a:
                ans+=(b_num-b[j])
            elif not "R" in a:
                ans+=(r_num-r[j])
            else:
                ans+=(g_num-g[j])
            continue
        else:
            if s[j+sa] in a: 
                if not "B" in a:
                    ans+=(b_num-b[j])
                elif not "R" in a:
                    ans+=(r_num-r[j])
                else:
                    ans+=(g_num-g[j])
            else:
                ans+=c(s[j+sa],j)-1
print(ans)

Solution (Python):
import bisect

n = int(input())
s = input()
rgb = [[],[],[]]
for i in range(n):
  if s[i] == 'R':
    rgb[0].append(i)
  elif s[i] == 'G':
    rgb[1].append(i)
  else:
    rgb[2].append(i)
x = []
for i in range(3):
  x.append((len(rgb[i]),i))
x.sort()
y = []
for i in range(3):
  y.append(rgb[x[i][1]])
a = 0
for y0 in y[0]:
  for y1 in y[1]:
    b = (y0+y1)//2
    if abs(b-y0) == abs(b-y1):
      r = bisect.bisect_left(y[2],b)
      if r < len(y[2]) and y[2][r] == b:
        a += 1
    b = min(y0,y1)-abs(y0-y1)
    if b >= 0:
      r = bisect.bisect_left(y[2],b)
      if r < len(y[2]) and y[2][r] == b:
        a += 1
    b = max(y0,y1)+abs(y0-y1)
    if b < n:
      r = bisect.bisect_left(y[2],b)
      if r < len(y[2]) and y[2][r] == b:
        a += 1
print(len(y[0])*len(y[1])*len(y[2])-a)

Solution (Python):
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions


N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()
r = S.count('R')
g = S.count('G')
b = S.count('B')
t = r * g * b
for el in range(N):
    end = N - 1 - el
    tt = min([el, end])
    for el2 in range(1, tt+1):
        if S[el - el2] != S[el] and S[el + el2] != S[el] and S[el - el2] != S[el + el2]:
            t -= 1
print(t)

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

NR = [-1 for _ in range(N)]
NG = [-1 for _ in range(N)]
NB = [-1 for _ in range(N)]

R_pos = []
G_pos = []
B_pos = []

r = 0
g = 0
b = 0
for i in range(N):
    if S[i] == 'R':
        r += 1
        R_pos.append(i)
    elif S[i] == 'G':
        g += 1
        G_pos.append(i)
    else:
        b += 1
        B_pos.append(i)

    NR[i] = r
    NG[i] = g
    NB[i] = b

ans = 0
for i_ in R_pos:
    for j_ in G_pos:
        if i_ <= j_:
            i = i_
            j = j_
        else:
            i = j_
            j = i_

        ans += NB[N - 1] - NB[j]
        if j + (j - i) < N:
            ans -= NB[j + (j - i)] - NB[j + (j - i) - 1]

    for j_ in B_pos:
        if i_ <= j_:
            i = i_
            j = j_
        else:
            i = j_
            j = i_

        ans += NG[N - 1] - NG[j]
        if j + (j - i) < N:
            ans -= NG[j + (j - i)] - NG[j + (j - i) - 1]

for i_ in G_pos:
    for j_ in B_pos:
        if i_ <= j_:
            i = i_
            j = j_
        else:
            i = j_
            j = i_

        ans += NR[N - 1] - NR[j]
        if j + (j - i) < N:
            ans -= NR[j + (j - i)] - NR[j + (j - i) - 1]

print(ans)

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

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

rgb = 0
for j in range(1,n//2+1):
  for i in range(n-2):
    try:
      arr = sorted([s[i],s[i+j],s[i+j*2]])
      if arr == ["B","G","R"]:
        rgb +=1
    except:
      continue

ans = r*g*b - rgb 
print(ans)

Solution (Python):




n = int(input())

def check(i, j, col, occ):
    c = occ[n-1]-occ[j];
    if (j+j-i<=n-1 and rgb[j+(j-i)]==col): 
        c-=1
    return c

rgb = list(input())

red, blue, green = [0]*n,[0]*n,[0]*n 

if (rgb[0]=='R'): 
    red[0]+=1;
elif (rgb[0]=='B'): 
    blue[0]+=1;
elif (rgb[0]=='G'): 
    green[0]+=1;
for i in range(1,n):
    red[i] = red[i-1];
    blue[i] = blue[i-1];
    green[i] = green[i-1];
    if (rgb[i]=='R'): red[i]+=1;
    elif (rgb[i]=='B'): blue[i]+=1;
    elif (rgb[i]=='G'): green[i]+=1;


ans = 0;
for i in range(0,n-2) :
    for j in range(i+1,n-1) :
        if (rgb[j]==rgb[i]): 
            continue
        if ('G' != rgb[i] and 'G' != rgb[j]): 
            ans += check(i,j,'G',green);
        elif ('B' != rgb[i] and 'B' != rgb[j]): 
            ans += check(i,j,'B',blue);
        elif ('R' != rgb[i] and 'R' != rgb[j]):
            ans += check(i,j,'R',red);

print(ans)

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

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

hiku = 0;
for j in range(2,n) :
    if j-1 < n-j :
        for i in range(1,j) :
            if (not(s[j-i-1]==s[j-1])) and (not(s[j-i-1]==s[j+i-1])) and (not(s[j-1]==s[j+i-1])) :
                hiku += 1;
    else :
        for i in range(1,n-j+1) :
            if (not(s[j-i-1]==s[j-1])) and (not(s[j-i-1]==s[j+i-1])) and (not(s[j-1]==s[j+i-1])) :
                hiku += 1;

print(len(r)*len(g)*len(b)-hiku);

Solution (Python):

import sys


def solve(N: int, S: str):
    r = 0
    g = 0
    b = 0
    sub = 0
    for i in range(N):
        if S[i] == "R":
            r += 1
            for j in range(i + 1, N):
                if S[j] == "G":
                    if j * 2 - i < N and S[j * 2 - i] == "B":
                        sub += 1
                elif S[j] == "B":
                    if j * 2 - i < N and S[j * 2 - i] == "G":
                        sub += 1
        elif S[i] == "G":
            g += 1
            for j in range(i + 1, N):
                if S[j] == "R":
                    if j * 2 - i < N and S[j * 2 - i] == "B":
                        sub += 1
                elif S[j] == "B":
                    if j * 2 - i < N and S[j * 2 - i] == "R":
                        sub += 1
        else:
            b += 1
            for j in range(i + 1, N):
                if S[j] == "R":
                    if j * 2 - i < N and S[j * 2 - i] == "G":
                        sub += 1
                elif S[j] == "G":
                    if j * 2 - i < N and S[j * 2 - i] == "R":
                        sub += 1

    print(r * g * b - sub)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
s_list = list(s)
result = s_list.count("R") * s_list.count("G") *s_list.count("B") 
for i in range(1,n):
  left = s_list[:i]
  right = s_list[i+1:]
  for j in range(min(len(left),len(right))):
    if s[i] != s[i-j-1] and s[i] != s[i + j + 1] and s[i-j-1] != s[i + j + 1]:
      result -= 1
    
print(result)

Solution (Python):
n = int(input())
s = list(input())
r = [0] * n
g = [0] * n
b = [0] * n
rcnt = 0
gcnt = 0
bcnt = 0
for i in range(n):
    r[i] += r[i-1]
    g[i] += g[i-1]
    b[i] += b[i-1]
    if s[i] == 'R':
        r[i] += 1
        rcnt += 1
    if s[i] == 'G':
        g[i] += 1
        gcnt += 1
    if s[i] == 'B':
        b[i] += 1
        bcnt += 1

ans = 0
for i in range(n-1):
    for j in range(i+1,n-1):
        if s[i] == 'R':
            if s[j] == 'G':
                ans += b[-1] - b[j]
                if j + (j-i) < n:
                    if s[j + (j-i)] == 'B':
                        ans -= 1
            if s[j] == 'B':
                ans += g[-1] - g[j]
                if j + (j-i) < n:
                    if s[j + (j-i)] == 'G':
                        ans -= 1
        if s[i] == 'G':
            if s[j] == 'R':
                ans += b[-1] - b[j]
                if j + (j - i) < n:
                    if s[j + (j-i)] == 'B':
                        ans -= 1
            if s[j] == 'B':
                ans += r[-1] - r[j]
                if j + (j-i) < n:
                    if s[j + (j-i)] == 'R':
                        ans -= 1
        if s[i] == 'B':
            if s[j] == 'R':
                ans += g[-1] - g[j]
                if j + (j-i) < n:
                    if s[j + (j-i)] == 'G':
                        ans -= 1
            if s[j] == 'G':
                ans += r[-1] - r[j]
                if j + (j-i) < n:
                    if s[j + (j-i)] == 'R':
                        ans -= 1
print(ans)

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

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

res = r * g * 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] and S[j] != S[k] and S[i] != S[k]:
            res -= 1

print(res)

Solution (Python):
n = int(input())
s=input()
r = [0]*n
g = [0]*n
b = [0]*n
for i in range(n):
    if s[i] == "R":
        r[i] = r[i - 1] + 1
        g[i] = g[i - 1]
        b[i] = b[i - 1]
    elif s[i] == "G":
        r[i] = r[i - 1]
        g[i] = g[i - 1] + 1
        b[i] = b[i - 1]
    else:
        r[i] = r[i - 1]
        g[i] = g[i - 1]
        b[i] = b[i - 1] + 1
ans=0
for k in range(n-1,-1,-1):
    for j in range(k - 1, -1, -1):
        if s[k]==s[j]:continue
        i = {"R", "G", "B"} - {s[k], s[j]}
        if "R" in i:
            ans += r[j]
        elif "G" in i:
            ans += g[j]
        else:
            ans += b[j]
        if 2 * j - k >= 0 and s[2 * j - k] in i:
            ans -= 1
print(ans)

Solution (Python):
def main():
    N = int(input())
    S = input()
    R, G, B = [0] * (N + 1), [0] * (N + 1), [0] * (N + 1)
    for i in range(N - 1, -1, -1):
        R[i], G[i], B[i] = R[i + 1], G[i + 1], B[i + 1]
        if S[i] == 'R':
            R[i] += 1
        elif S[i] == 'G':
            G[i] += 1
        else:
            B[i] += 1
    ans = 0
    for i in range(N):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            if j + (j - i) + 1 < N + 1:
                if S[i] + S[j] == 'RG':
                    ans += B[j + 1]
                    if B[j + (j - i) + 1] != B[j + (j - i)]:
                        ans -= 1
                elif S[i] + S[j] == 'GR':
                    ans += B[j + 1]
                    if B[j + (j - i) + 1] != B[j + (j - i)]:
                        ans -= 1
                elif S[i] + S[j] == 'RB':
                    ans += G[j + 1]
                    if G[j + (j - i) + 1] != G[j + (j - i)]:
                        ans -= 1
                elif S[i] + S[j] == 'BR':
                    ans += G[j + 1]
                    if G[j + (j - i) + 1] != G[j + (j - i)]:
                        ans -= 1
                elif S[i] + S[j] == 'GB':
                    ans += R[j + 1]
                    if R[j + (j - i) + 1] != R[j + (j - i)]:
                        ans -= 1
                elif S[i] + S[j] == 'BG':
                    ans += R[j + 1]
                    if R[j + (j - i) + 1] != R[j + (j - i)]:
                        ans -= 1
            else:
                if S[i] + S[j] == 'RG':
                    ans += B[j + 1]
                elif S[i] + S[j] == 'GR':
                    ans += B[j + 1]
                elif S[i] + S[j] == 'RB':
                    ans += G[j + 1]
                elif S[i] + S[j] == 'BR':
                    ans += G[j + 1]
                elif S[i] + S[j] == 'GB':
                    ans += R[j + 1]
                elif S[i] + S[j] == 'BG':
                    ans += R[j + 1]
    print(ans)


if __name__ == '__main__':
    main()

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

Bcount = S.count("B")
for i in range(0,N-1):
    for j in range(i+1,N):
        if (S[i] == "R" and S[j] == "G") or (S[i] == "G" and S[j] == "R") :
            count += Bcount
            if j+(j-i) <N  and S[j+(j-i)] == "B":
                count  -=1
            if i-(j-i) >=0 and S[i-(j-i)] =="B":
                count -=1
            if (i+j)%2 ==0 and S[int((i+j)/2)] == "B":
                count -=1
print(count)

Solution (Python):
'''


39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB


'''

l=[]
q=[]
w=[]
n=int(input())
s=input().rstrip()
x=list(s)
for i in range(0,n):
    if x[i]=='R':
        l.append(i)
    elif x[i]=='G':
        q.append(i);
    else:
        w.append(i);
total = 0;
for i in range(0,len(l)):
    for j in range(0,len(q)):
        A=abs(l[i]-q[j]);
        H=[]
        H.append(l[i])
        H.append(q[j]);
        H.sort();
        B=H[0]-A;
        C=H[1]+A;
        D=-1;
        if A%2==0:
            D=H[0]+(A//2);
        I=0;
        J=len(w)-1;
        V=0;
        while(I<=J):
            m=I+(J-I)//2;
            if w[m]==B:
                V+=1;
                break;
            elif w[m]>B:
                J=m-1;
            else:
                I=m+1;
        B=C;
        I=0;
        J=len(w)-1
        while(I<=J):
            m=I+(J-I)//2;
            if w[m]==B:
                V+=1;
                break;
            elif w[m]>B:
                J=m-1;
            else:
                I=m+1;
        if D!=-1:
            B=D;
            I=0;
            J=len(w)-1
            while(I<=J):
                m=I+(J-I)//2;
                if w[m]==B:
                    V+=1;
                    break;
                elif w[m]>B:
                    J=m-1;
                else:
                    I=m+1;            
        total+=(len(w)-V);
print(total)

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

ans = r * g * b
if ans == 0:
    print("0")
else:
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            sub = j - i
            sub2 = (n - 1) - j
            if sub <= sub2:
                if s[i] != s[j] and s[j] != s[j + sub] and s[i] != s[j + sub]:
                    ans -= 1
    print(ans)

Solution (Python):
n= int(input())
ss=input()
rs = set()
gs = set()
bs = set()
 
for i, s in enumerate(ss):
    if s == "R":
        rs.add(i)
    elif s == "G":
        gs.add(i)
    else:
        bs.add(i)
ans = 0
if len(gs) > len(bs) and len(gs) > len(rs):
    tmp = gs
    gs = bs
    bs = tmp
elif len(rs) > len(bs) and len(rs) > len(gs):
    tmp =rs
    rs = bs
    bs = tmp
    
for r in rs:
    for g in gs:
        c = 0
        if r+r-g in bs:
            c+=1
        if g+g-r in bs:
            c+=1
        if (g+r)/2 in bs:
            c+=1
        ans += len(bs) -c
print(ans)

Solution (Python):
import sys
from pprint import pprint

from collections import Counter
from math import factorial

def nCr(n, r):
    return factorial(n) // factorial(r) // factorial(n - r)

N = int(sys.stdin.readline())
S = sys.stdin.readline().strip()


counter = Counter(S)
if len(counter) != 3:
    print("0")
    sys.exit()
rgb_count = 0

max_width = len(S) // 2
for width in range(1, max_width+1):
    max_left = len(S) - 2 * width
    
    for i in range(max_left):
        if len(set([S[i], S[i+width], S[i+2*width]])) == 3:
            rgb_count += 1


all_count = nCr(counter["R"], 1) * nCr(counter["G"], 1) * nCr(counter["B"], 1) 

print(all_count - rgb_count)

Solution (Python):
N = int(input())
S = list(input())
ans = 0
R, G, B = 0, 0, 0

for char in S:
    if char == "R":
        R += 1
    elif char == "G":
        G += 1
    else:
        B += 1

ans = R*G*B

for i in range(len(S)):
    for j in range(i+1, len(S)):
        if S[i] != S[j]:
            i2j = j - i
            try:
                if not S[j+i2j] in [S[i], S[j]]:
                    ans -= 1
            except:
                break

print(ans)

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

s = list(input())

res = 0

s_dif_g = [0]*n
s_dif_r = [0]*n
s_dif_b = [0]*n

for i in range(n):
    s_dif_r[i] = s[i::].count('R')
    s_dif_g[i] = s[i::].count('G')
    s_dif_b[i] = s[i::].count('B')

sample = ["R","G","B"]
for i in range(n):
    fst = s[i]
    for j in range(i+1,n-1):
        if(s[j] != fst):
            scd = s[j]
            for x in sample:
                if(x != fst and x != scd):
                    if(x == "R"):
                        res += s_dif_r[j+1]
                    if(x == "G"):
                        res += s_dif_g[j+1]
                    if(x == "B"):
                        res += s_dif_b[j+1]
                    if(2*j-i < n):    
                        if(s[2*j-i] == x):
                            res -= 1
            
            

print(res)

Solution (Python):
def main():
    n=int(input())
    s=input()
    ans=0
    d=dict()
    dr=[0]*n
    dg=[0]*n
    db=[0]*n
    mr=[0]*n
    mg=[0]*n
    mb=[0]*n

    for i in range(n):
        if s[i]=='R':
            dr[i]=1
            mr[i]=1
        if s[i]=='G':
            dg[i]=1
            mg[i]=1
        if s[i]=='B':
            db[i]=1
            mb[i]=1

    for i in range(n-1):
        mr[n-2-i]+=mr[n-1-i]
        mg[n-2-i]+=mg[n-1-i]
        mb[n-2-i]+=mb[n-1-i]

    for i in range(n-2):
        for j in range(i,n-1):
            if s[i]==s[j]:
                continue
            if s[i]=='R' and s[j]=='G':
                k=1
            elif s[i]=='R' and s[j]=='B':
                k=2
            elif s[i]=='G' and s[j]=='R':
                k=1
            elif s[i]=='G' and s[j]=='B':
                k=0
            elif s[i]=='B' and s[j]=='R':
                k=2
            elif s[i]=='B' and s[j]=='G':
                k=0
            
            
            if k==0:
                ans+=mr[j+1]
                if (j+(j-i))<n:
                    ans-=dr[j+(j-i)]
            if k==1:
                ans+=mb[j+1]
                if (j+(j-i))<n:
                    ans-=db[j+(j-i)]
            if k==2:
                ans+=mg[j+1]
                if (j+(j-i))<n:
                    ans-=dg[j+(j-i)]

    print(ans)
        



main()

Solution (Python):

import sys
input = sys.stdin.readline





def main():
    n = int(input())
    rgb = input()
    ans = 0
    for moji in ["RGB", "RBG", "BGR", "BRG", "GBR", "GRB"]:
        ans += search(moji, rgb)
    print(ans)

def search(moji, kensaku):
    n = len(kensaku)
    memo1 = []
    r = 0
    g = 0
    b = 0
    red = 0
    for i in range(n):
        if kensaku[i] == moji[0]:
            memo1.append(i)
        elif kensaku[i] == moji[1]:
            for num in memo1:
                check = num + 2 * (i - num)
                if check >= n:
                    continue
                else:
                    if kensaku[check] == moji[2]:
                        red += 1
    for i in range(n):
        if kensaku[i] == moji[0]:
            r += 1
        elif kensaku[i] == moji[1]:
            g += r
        elif kensaku[i] == moji[2]:
            b += g
    ans = b - red
    return ans
    
if __name__ == '__main__':
    main()

Solution (Python):


def main():
    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)
        elif s[i] == 'B':
            b.append(i+1)

    count = len(r) * len(g) * len(b)
    for r_num in r:
        for g_num in g:
            dif = abs(r_num - g_num)
            i, j = tuple(sorted([r_num, g_num]))

            if i - dif > 0:
                if s[i - dif - 1] == 'B':
                    count -= 1
            if j + dif <= n:
                if s[j + dif - 1] == 'B':
                    count -= 1
            if dif % 2 == 0:
                if s [i + dif // 2 - 1] == 'B':
                    count -= 1

    print(count)

if __name__ == '__main__':
    main()

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

s_l = {}
for i in range(N-1):
    t = {}
    s = S[i+1:]
    t['s'] = s
    t['R'] = s.count('R')
    t['G'] = s.count('G')
    t['B'] = s.count('B')
    s_l[i] = t

s = 0
for a in range(N-2):
    s1 = S[a]
    for b in range(a+1, N-1):
        s2 = S[b]
        if s1 == s2:
            continue
        n = s_l[b]['s']
        pat = ['R', 'G', 'B']
        pat.remove(s1)
        pat.remove(s2)
        count = s_l[b][pat[0]]
        d = b - a
        if len(n) > d-1 and n[d-1] == pat[0]:
            count -= 1
        s += count

print(s)

Solution (Python):
n=int(input())
s=input()
if n<=3:
    print(0)
    exit()
r=set()
g=set()
b=set()
for i,c in enumerate(s):
    if c=='R':
        r.add(i)
    elif c=='G':
        b.add(i)
    else:
        g.add(i)
ans=0
lb=len(b)
for i in r:
    for j in g:
        t1=-1
        if abs(i-j)%2==0:
            t1=min(i,j)+abs(i-j)//2
        t2=max(i,j)+abs(i-j)
        t3=min(i,j)-abs(i-j)
        tc=0
        if t1 in b:
            tc+=1
        if t2 in b:
            tc+=1
        if t3 in b:
            tc+=1
        ans+=max(0,lb-tc)
print(ans)

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

dic = {"R":0, "G":0, "B":0}
for color in S:
    dic[color] += 1
count = dic["R"] * dic["G"] * dic["B"]

for step in range(1, (N - 1) // 2 + 1):
    for st in range(N - 2 * step):
        a, b, c = S[st], S[st + step], S[st + step * 2]
        if a != b and b != c and a != c:
            count -= 1
print(count)

Solution (Python):
N=input()
S=input()
N=int(N)
ans=0
ind=[]
tmp={"R":0,"G":0,"B":0}
for i in S:
    tmp[i]+=1
    ind.append(tmp.copy())

for i in range(0,N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            continue
        for k in tmp.keys():
            if k!=S[i] and k!=S[j]:
                col=k
                break
        
        if 2*j-i <N and S[j+j-i]==col:
            ans-=1
        ans+=(ind[-1][col]-ind[j][col])
print(ans)

Solution (Python):
n = int(input())
s = input()
r = [0] * n
g = [0] * n
b = [0] * n
R, G, B = 0, 0, 0
for i in range(n - 1, -1, -1):
    if s[i] == 'R': R -= -1
    elif s[i] == 'G': G -= -1
    else: B -= -1
    r[i] = R
    g[i] = G
    b[i] = B




ans = 0
for i in range(1, n - 1):
    for j in range(i):
        if s[j] != s[i]:
            t = i + i - j
            if s[i] == 'R':
                if s[j] == 'G':
                    ans += b[i + 1]
                    if t < n and s[t] == 'B': ans += -1
                else:
                    ans += g[i + 1]
                    if t < n and s[t] == 'G': ans += -1
            elif s[i] == 'G':
                if s[j] == 'R':
                    ans += b[i + 1]
                    if t < n and s[t] == 'B': ans += -1
                else:
                    ans += r[i + 1]
                    if t < n and s[t] == 'R': ans += -1
            elif s[i] == 'B':
                if s[j] == 'R':
                    ans += g[i + 1]
                    if t < n and s[t] == 'G': ans += -1
                else:
                    ans += r[i + 1]
                    if t < n and s[t] == 'R': ans += -1
print(ans)

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

col_lis = [0 if c == "R" else 1 if c == "G" else 2 for c in S]

ans = 0

r_count = 0
g_count = 0
b_count = 0

color_table = []

for color in col_lis:
    if color == 0:
        r_count += 1
    elif color == 1:
        g_count += 1
    else:
        b_count += 1
    color_table.append([r_count, g_count, b_count])

ans = 0

for center in range(1, N - 1):
    for right in range(center + 1, N):
        if col_lis[right] == col_lis[center]:
            continue
        left = 3 - col_lis[right] - col_lis[center]
        ans += color_table[center - 1][left]
        if center * 2 - right >= 0 and col_lis[center * 2 - right] == left:
            ans -= 1


print(ans)

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

ans = 0
for i in range(1,n-1):
    sr = 0
    sg = 0
    sb = 0
    er = 0
    eg = 0
    eb = 0

    for j in range(i):
        if s[j] != s[i]:
            if s[j] == "R":
                sr += 1
            elif s[j] == "G":
                sg += 1
            else:
                sb += 1
        pass
    for j in range(i+1,n):
        if s[j] != s[i]:
            if s[j] == "R":
                er += 1
            elif s[j] == "G":
                eg += 1
            else:
                eb += 1
    ans += sr*eb + sr*eg + sg*eb + sg*er + sb*er + sb*eg
    for j in range(min(i,n-i-1)):
        if s[i+j+1] != s[i-j-1] and s[i+j+1] != s[i] and s[i] != s[i-j-1]:
            ans -= 1
    

print(ans)

Solution (Python):
n=int(input())
s=input()
r=[0]*n
g=[0]*n
b=[0]*n
for i in range(n):
    if s[i]=="R":
        r[i]+=1
    elif s[i]=="G":
        g[i]+=1
    elif s[i]=="B":
        b[i]+=1
import itertools as it
acr=list(it.accumulate(r))
acg=list(it.accumulate(g))
acb=list(it.accumulate(b))


ans=0
memo=set()
for j in range(n):
    if g[j]==1:
        ans+=acr[j]*(acb[-1]-acb[j])
        ans+=acb[j]*(acr[-1]-acr[j])
        for x in range(1,min(j,n-j-1)+1):
            if j-x>=0 and s[j-x]!=s[j+x] and s[j-x]!="G" and s[j+x]!="G":
                ans-=1
    if r[j]==1:
        ans+=acg[j]*(acb[-1]-acb[j])
        ans+=acb[j]*(acg[-1]-acg[j])
        for x in range(1,min(j,n-j-1)+1):
            if j-x>=0 and s[j-x]!=s[j+x] and s[j-x]!="R" and s[j+x]!="R":
                ans-=1
    if b[j]==1:
        ans+=acr[j]*(acg[-1]-acg[j])
        ans+=acg[j]*(acr[-1]-acr[j])
    
        for x in range(1,min(j,n-j-1)+1):
            if j-x>=0 and s[j-x]!=s[j+x] and s[j-x]!="B" and s[j+x]!="B":
                ans-=1

print(ans)

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
s = input()

from bisect import bisect_left as bl
b = []
r = []
g = []
gg = 0
rr = 0
bb = 0
for i in range(n):

    if s[i] == "G":
        gg += 1
    elif s[i] == "B":
        bb += 1
    else:
        rr += 1
    
    r.append(rr)
    b.append(bb)
    g.append(gg)

brg = [b,r,g]
ans = 0
for i in range(1,n-1):
    if s[i] == "B":
        ima1 = 1
        ima2 = 2
    elif s[i] == "R":
        ima1 = 0
        ima2 = 2
    else:
        ima1 = 0
        ima2 = 1
    ans += brg[ima1][i-1]*(brg[ima2][n-1]- brg[ima2][i])
    ans += brg[ima2][i-1]*(brg[ima1][n-1]- brg[ima1][i])
for i in range(1,n-1):
    for k in range(i):
        tmp = i-k
        if i + tmp > n-1:
            continue
        if s[k] == s[i]:
            continue
        if s[i] == s[i+tmp]:
            continue
        if s[k] == s[i+tmp]:
            continue
        ans -= 1

print(ans)