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



def main():
    N = int(input())
    S = input()
    S = [-1 if x == "R" else (0 if x == "G" else 1) for x in S]

    n_right_RGB = [None] * N

    n_r = 0
    n_g = 0
    n_b = 0
    for i in range(N):
        if S[i] == -1:
            n_r += 1
        elif S[i] == 0:
            n_g += 1
        else:
            n_b += 1
    
    for i in range(N):
        if S[i] == -1:
            n_r -= 1
        elif S[i] == 0:
            n_g -= 1
        else:
            n_b -= 1

        n_right_RGB[i] = [n_r, n_g, n_b]
    
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == -1 and S[j] == 0:
                ans += n_right_RGB[j][2]
                if 2*j-i < N and S[2*j-i] == 1:
                    ans -= 1
            elif S[i] == -1 and S[j] == 1:
                ans += n_right_RGB[j][1]
                if 2*j-i < N and S[2*j-i] == 0:
                    ans -= 1
            elif S[i] == 0 and S[j] == 1:
                ans += n_right_RGB[j][0]
                if 2*j-i < N and S[2*j-i] == -1:
                    ans -= 1
            elif S[i] == 0 and S[j] == -1:
                ans += n_right_RGB[j][2]
                if 2*j-i < N and S[2*j-i] == 1:
                    ans -= 1
            elif S[i] == 1 and S[j] == -1:
                ans += n_right_RGB[j][1]
                if 2*j-i < N and S[2*j-i] == 0:
                    ans -= 1
            elif S[i] == 1 and S[j] == 0:
                ans += n_right_RGB[j][0]
                if 2*j-i < N and S[2*j-i] == -1:
                    ans -= 1
    
    return ans


print(main())

Solution (Python):
N = int(input())
S = (input())
sum_count = 0
RGB_counter = [0,0,0]
total_pattern = 0


for i in range(3):
    if S[N-i-1] == "R":
        RGB_counter[0] += 1
    elif S[N-i-1] == "G":
        RGB_counter[1] += 1
    else:
        RGB_counter[2] += 1



for i in range(N-3):
    if S[N-i-4] == "R":
        RGB_counter[0] += 1
        if min(RGB_counter) == 0:
            pass
        else:
            total_pattern = RGB_counter[1] * RGB_counter[2]
 
            for j in range((i + 3) // 2):
                if (S[N-i-4+(j+1)] == "B" and S[N-i-4+2*(j+1)] == "G") or (S[N-i-4+(j+1)] == "G" and S[N-i-4+2*(j+1)] == "B"):
                    total_pattern -= 1
    elif S[N-i-4] == "G":
        RGB_counter[1] += 1
        if min(RGB_counter) == 0:
            pass
        else:
            total_pattern = RGB_counter[0] * RGB_counter[2]

            for j in range((i + 3) // 2):
                if (S[N-i-4+(j+1)] == "R" and S[N-i-4+2*(j+1)] == "B") or (S[N-i-4+(j+1)] == "B" and S[N-i-4+2*(j+1)] == "R"):
                    total_pattern -= 1
    else:
        RGB_counter[2] += 1
        if min(RGB_counter) == 0:
            pass
        else:
            total_pattern = RGB_counter[0] * RGB_counter[1]

            for j in range((i + 3) // 2):
                if (S[N-i-4+(j+1)] == "R" and S[N-i-4+2*(j+1)] == "G") or (S[N-i-4+(j+1)] == "G" and S[N-i-4+2*(j+1)] == "R"):
                    total_pattern -= 1  
    sum_count += total_pattern

print(sum_count)

Solution (Python):
n=int(input())
S=input()
RGB=[[] for i in range(3)]
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)
L=[len(RGB[0]),len(RGB[1]),len(RGB[2])]
m=L.index(max(L))
A=[]
for i in range(3):
    if i==m:
        continue
    else:
        A.append(i)
C=RGB[A[0]]
D=RGB[A[1]]
E=RGB[m]

ans=0
for i in range(L[A[0]]):
    for j in range(L[A[1]]):
        r=C[i]
        g=D[j]
        d=abs(r-g)
        cnt=0
        if d%2==0:
            if (r+g)//2 in E:
                cnt+=1
        u=max(r,g)+d
        l=min(r,g)-d
        if 0<=u<=n-1:
            if u in E:
                cnt+=1
        if 0<=l<=n-1:
            if l in E:
                cnt+=1
        ans+=L[m]-cnt
print(ans)

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

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

r = [0]*n
g = [0]*n
b = [0]*n

rc,gc,bc = 0,0,0

for i in range(n-1,-1,-1):
    if s[i] == "R":
        rc += 1
    elif s[i] == "G":
        gc += 1
    else:
        bc += 1

    r[i] = rc
    g[i] = gc
    b[i] = bc

if n in [1,2]:
    print(0)
else:
    res = 0
    for i in range(1,n-1):
        for j in range(i):
            if s[i] == s[j]:
                continue
            if s[i] != "G" and s[j] != "G":
                res += g[i+1]
                if i+i-j < n and s[i+i-j] == "G":
                    res -= 1
            if s[i] != "B" and s[j] != "B":
                res += b[i+1]
                if i+i-j < n and s[i+i-j] == "B":
                    res -= 1
            if s[i] != "R" and s[j] != "R":
                res += r[i+1]
                if i+i-j < n and s[i+i-j] == "R":
                    res -= 1
            
    print(res)

Solution (Python):

import itertools
import math
import sys

input=sys.stdin.readline
N = int(input())
S = list(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
    else:
        B[i] += 1

for i in range(N-1):
    R[i+1] += R[i]
    G[i+1] += G[i]
    B[i+1] += B[i]
ans = 0

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

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

R = [i for i in range(N) if S[i] == "R"]
G = [i for i in range(N) if S[i] == "G"]
B = [i for i in range(N) if S[i] == "B"]

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

print(ans)

Solution (Python):
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

def main():
    n = int(input())
    s = input()
    r = [0] * n
    g = [0] * n
    b = [0] * n
    if s[0] == 'R':
        r[0] = 1
    elif s[0] == 'G':
        g[0] = 1
    else:
        b[0] = 1
    for i, v in enumerate(s[1:]):
        if v == 'R':
            r[i+1] = 1
        elif v == 'G':
            g[i+1] = 1
        else:
            b[i+1] = 1
        r[i+1] += r[i]
        g[i+1] += g[i]
        b[i+1] += b[i]
    ans = 0
    for j, v in enumerate(s[:-1]):
        if j == 0:
            continue
        if v == 'R':
            ans += g[j-1] * (b[-1] - b[j]) + b[j-1] * (g[-1] - g[j])
        elif v == 'G':
            ans += r[j-1] * (b[-1] - b[j]) + b[j-1] * (r[-1] - r[j])
        else:
            ans += r[j-1] * (g[-1] - g[j]) + g[j-1] * (r[-1] - r[j])
        for d in range(1, j + 1):
            i = j - d
            k = j + d
            if i < 0 or k >= n:
                break
            if s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
                ans -= 1
    print(ans)

main()

Solution (Python):
N=int(input())
S=input()
x=dict()
for i in range(N):
    s=S[i]
    if s in x:
        x[s]+=1
    else:
        x[s]=1
count=0
for i in range(1,N+1):
    for k in range(i+1,N+1):
        tmp=i+k
        if tmp%2==0:
            j=tmp//2
            if S[i-1]!=S[j-1] and S[i-1]!=S[k-1] and S[j-1]!=S[k-1]:
                count+=1
ans=1
cnt=0
for idx in x:
    cnt+=1
    ans=ans*x[idx]
if cnt!=3:
    print(0)
    exit()
ans=ans-count
print(ans)

Solution (Python):
from collections import Counter

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

dup=0
RGB=set(('R','G','B'))

for width in range(N//2+10):
    for i in range(N):
        try:
            if set((S[i],S[i+width],S[i+width*2]))==RGB:
                dup+=1
        except IndexError:
            break

c=Counter(S)
retval=c['R']*c['G']*c['B']-dup
print(retval)

Solution (Python):
import math
import fractions
import collections
import itertools
from collections import deque
N=int(input())
S=input()
leftRGB=[[0,0,0] for _ in range(N)]
rightRGB=[[0,0,0] for _ in range(N)]
cnt=0
buf=0
ch=""
R=0
G=0
B=0
for i in range(N):
    if S[i]=="R":
        R=R+1
    elif S[i]=="G":
        G=G+1
    else:
        B=B+1
    leftRGB[i][0]=R
    leftRGB[i][1] = G
    leftRGB[i][2] = B
R=0
G=0
B=0
for i in range(N):
    if S[N-1-i]=="R":
        R=R+1
    elif S[N-1-i]=="G":
        G=G+1
    else:
        B=B+1
    rightRGB[N-1-i][0]=R
    rightRGB[N-1-i][1] = G
    rightRGB[N-1-i][2] = B


for i in range(N):
    if (i!=0)and(i!=N-1):
        ch=S[i]
        if S[i]=="R":
            cnt=cnt+(leftRGB[i-1][1]*rightRGB[i+1][2])+(leftRGB[i-1][2]*rightRGB[i+1][1])
            for j in range(min(i,N-1-i)):
                if (S[i-j-1]=="G")and(S[i+j+1]=="B"):
                    cnt=cnt-1
                if (S[i-j-1]=="B")and(S[i+j+1]=="G"):
                    cnt=cnt-1
        elif S[i]=="G":
            cnt = cnt + (leftRGB[i - 1][0] * rightRGB[i + 1][2])+(leftRGB[i - 1][2] * rightRGB[i + 1][0])
            for j in range(min(i,N-1-i)):
                if (S[i-j-1]=="R")and(S[i+j+1]=="B"):
                    cnt=cnt-1
                if (S[i-j-1]=="B")and(S[i+j+1]=="R"):
                    cnt=cnt-1
        else:
            cnt = cnt + (leftRGB[i - 1][0] * rightRGB[i + 1][1])+(leftRGB[i - 1][1] * rightRGB[i + 1][0])
            for j in range(min(i,N-1-i)):
                if (S[i-j-1]=="R")and(S[i+j+1]=="G"):
                    cnt=cnt-1
                if (S[i-j-1]=="G")and(S[i+j+1]=="R"):
                    cnt=cnt-1
        
        
print(cnt)

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

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

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

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

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

ans=0
for i in range(n):
    if R[i]==1:
        lG=sum(G[:i])
        lB=sum(B[:i])
        rG=sum(G[i:])
        rB=sum(B[i:])
        ans+=(lG+rG)*(lB+rB)
        for j in range(1,n//2+1):
            if R[i]==1:
                if 0<=i-j<n and 0<=i+j<n:
                    if G[i-j]==1 and B[i+j]==1:
                        ans-=1
                    if B[i-j]==1 and G[i+j]==1:
                        ans-=1
                if 0<=i-2*j<n:
                    if G[i-j]==1 and B[i-2*j]==1:
                        ans-=1
                    if B[i-j]==1 and G[i-2*j]==1:
                        ans-=1
                if 0<=i+2*j<n:
                    if G[i+j]==1 and B[i+2*j]==1:
                        ans-=1
                    if B[i+j]==1 and G[i+2*j]==1:
                        ans-=1

print(ans)

Solution (Python):
n = int(input())
s = input()
chkR = 0
chkB = 0
chkG = 0
if n == 1 or n==2:
  print(0)
  exit()
  
for i in range(n):
  if s[i] =='R':
    chkR+=1
  if s[i] =='B':
    chkB+=1
  if s[i] =='G':
    chkG+=1
    
ans = chkR*chkB*chkG

m = (n-3)//2
for i in range(m+1):
  for j in range(n-2-2*i):
    if s[j]!=s[j+i+1] and s[j+i+1]!=s[j+2*i+2] and s[j+2*i+2]!=s[j]:
      ans -= 1
      
print(ans)

Solution (Python):
N = int(input())
S = input()
R = 0
G = 0
B = 0
ans = 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 i in range(int((N+1)/2)):
    for j in range(i,N-i):
        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 = S.count("R")
g = S.count("G")
b = S.count("B")
ans = r*g*b
for i in range(1, (N-1)//2+1):
    for j in range(N-i*2):
        if not (S[j] == S[j+i] or S[j] == S[j+i*2] or S[j+i] == S[j+i*2]):
            ans -= 1
print(ans)

Solution (Python):
import sys













readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

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

    from collections import Counter

    cntr = Counter()
    ans = 0

    for i, ch in enumerate(s):
        cntr[ch] += 1
        if ch == "R":
            ans += cntr["G"] * cntr["B"]
        if ch == "G":
            ans += cntr["R"] * cntr["B"]
        if ch == "B":
            ans += cntr["R"] * cntr["G"]

        for j in range(1, i // 2 + 1):
            p = s[i - j]
            pp = s[i - j * 2]
            if ch != p and p != pp and pp != ch:
                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

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

rcount = 0
gcount = 0
bcount = 0

for s in ss:
  if s == "R":
    rcount += 1
  elif s == "G":
    gcount += 1
  else:
    bcount += 1

sumcount = rcount * gcount * bcount


dist = 1
while dist <= (n - 1) / 2:
  i = 0
  while i + 2 * dist < n:
    if ss[i] != ss[i + dist] and ss[i + dist] != ss[i + 2 * dist] and ss[i + 2 * dist] != ss[i]:
      sumcount -= 1
      
    i += 1
  dist += 1

print(sumcount)

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

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

out = 0
t = 1
while(n >= t*2+1):
    for i in range(n-2*t):

        
        

        if s[i] != s[i+t]:
            if s[i+t] != s[i+2*t]:
                if s[i] != s[i+2*t]:
                    ans -= 1
    t+=1

print(ans)

Solution (Python):
from sys import stdin 
n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()
answer = 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
answer += r*g*b
for i in range(n):
    for j in range(i):
        k = 2*j - i
        if k >= 0:
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                answer -= 1
print(answer)

Solution (Python):
n=int(input())
s=input()
ans=0
for i in range(n-2):
  for d in range(n):
    j=i+d
    k=i+2*d
    if k>=n:
      break
    if not (s[i]==s[j] or s[j]==s[k] or s[k]==s[i]):
      ans+=1
r=s.count('R')
g=s.count('G')
b=s.count('B')
ans+=r*(r-1)//2*g+r*(r-1)//2*b+g*(g-1)//2*r+g*(g-1)//2*b+b*(b-1)//2*r+b*(b-1)//2*g+r*(r-1)*(r-2)//6+g*(g-1)*(g-2)//6+b*(b-1)*(b-2)//6
print(n*(n-1)*(n-2)//6-ans)

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

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

print(allpattern-counter)

Solution (Python):
N = int(input())
S = str(input())
X = [0] * N

Sum1 = 0
Sum2 = 0
Sum3 = 0

for i in range(N):
    if S[i] == "R":
        X[i] = 1
        Sum1 += 1
    elif S[i] == "G":
        X[i] = 2
        Sum2 += 1
    elif S[i] == "B":
        X[i] = 4
        Sum3 += 1

Sum = Sum1 * Sum2 * Sum3

for i in range(N):
    for j in range(1, N):
        if i - j < 0 or N <= i + j:
            break
        else:
            if (X[i] + X[i - j] + X[i + j]) == 7:
                Sum -= 1

print(Sum)

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

cnt = [[0 for i in range(3)] for i in range(n)]
colors = 'RGB'

for i in range(1, n):
    for j in range(3):
        cnt[i][j] = cnt[i - 1][j]
        if j == colors.find(s[i - 1]):
            cnt[i][j] += 1

ans = 0
for i in range(2, n):
    color = colors.find(s[i])
    ans += cnt[i][(color + 1) % 3] * cnt[i][(color + 2) % 3]

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

print(ans)

Solution (Python):


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

culmR = []
culmG = []
culmB = []
cR = 0
cG = 0
cB = 0
for i in range(n):
    if s[i] == "R":
        cR += 1
    elif s[i] == "G":
        cG += 1
    else:
        cB += 1
    culmR.append(cR)
    culmG.append(cG)
    culmB.append(cB)


ans = 0
for i in range(n):
    for j in range(i+1, n):
        
        if i == j:
            pass
        else:
            count = 0
            
            if s[i] != s[j]:
                
                diff = j-i
                if "R" not in [s[i], s[j]]:
                    required = "R"
                    
                    
                    count = culmR[-1] - culmR[j]
                    
                    if (j+diff < n):
                        if s[j+diff] == "R":
                            count -= 1
                    
                elif "G" not in [s[i], s[j]]:
                    required = "G"
                    
                    
                    count = culmG[-1] - culmG[j]
                    
                    if (j+diff < n):
                        if s[j+diff] == "G":
                            count -= 1
                    
                else:
                    required = "B"
                    
                    
                    count = culmB[-1] - culmB[j]
                    
                    if (j+diff < n):
                        if s[j+diff] == "B":
                            count -= 1
                    
                ans += count
            
            else:
                pass
print(ans)

Solution (Python):

def main():
  
  

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

  import collections

  c = collections.Counter(S)

  total = c["B"]*c["G"]*c["R"]

  for d in range(1, N//2+1):
    for i in range(N-d-d):
      if len({S[i], S[i+d], S[i+d+d]})==3:
        total -= 1
  
  print(total)

main()

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

r = []
g = []
b = [0] * (n)

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

b_sum = sum(b)


ans = 0

for i in range(len(r)):
    for j in range(len(g)):
        tmp = abs(r[i] -g[j])
        ans += b_sum
        dek = tmp + max(r[i],g[j])
        if dek <= n-1:
            if b[dek] == 1:
                ans -= 1
                
        chi = min(r[i],g[j]) - tmp
        if chi >= 0:
            
            if b[chi] == 1:
                
                ans -= 1
                
        if tmp % 2 == 0:
            
            if b[min(r[i],g[j])+tmp//2] == 1:
                ans -= 1
print(ans)

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

def c(s1, s2, s3, i, s):
  cnt = 0
  L = S[:i]
  R = S[i+1:]
  
  if s == s1:
    lc = 0
    for l in L:
      if l == s2:
        lc += 1
    rc = 0
    for r in R:
      if r == s3:
        rc += 1
    cnt += lc * rc 
  for i, j in zip(R, L[::-1]):
    if s1 == s and i == s3 and j == s2:
      cnt -= 1
  
  return cnt

ans = 0
for i, s in enumerate(S):
  ans += c("R", "B", "G", i, s) 
  ans += c("R", "G", "B", i, s) 
  ans += c("B", "R", "G", i, s) 
  ans += c("B", "G", "R", i, s) 
  ans += c("G", "R", "B", i, s) 
  ans += c("G", "B", "R", i, s) 

print(ans)

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

rlist= []
glist = []
bset = set()
for i,s in enumerate(S):
    if s == "R":
        rlist.append(i)
    elif s=="G":
        glist.append(i)
    else:
        bset.add(i)
blist = [0 for i in range(n)]
cnt = 0
for i,s in enumerate(S[::-1]):
    if s == "B":
        cnt += 1
    blist[n-1-i] = cnt

res = 0
for r in rlist:
    for g in glist:
        res += len(bset)
        l, s = max(r,g), min(r,g)
        possible =  [s-(l-s), l+(l-s)]
        if (l-s) % 2 == 0: possible.append(s+(l-s)//2)
        for x in possible:
            if x in bset:
                res -= 1
print(res)

Solution (Python):
if __name__ == '__main__':
    from sys import stdin
    input = stdin.readline
    from collections import Counter

    import random

    n = int(input())
    s = input()[:-1]
    c = Counter(s)
    minus = 0
    for i in range(1,(n+1)//2):
        for j in range(n-2*i):
            if (s[j] != s[j+i]) and (s[j] != s[j+2*i]) and (s[j+i] != s[j+2*i]):
                
                minus += 1

    cnt = 1
    if not 'R' in c:
        print(0)
        exit()
    if not 'G' in c:
        print(0)
        exit()
    if not 'B' in c:
        print(0)
        exit()
    for value in c.values():
        cnt *= value
    print(int(cnt-minus))
"""
    true = 0
    true_cnt = 0
    for i in range(n):
        for j in range(i+1,n):
            for k in range(j+1,n):
                if (s[i] != s[j]) and (s[i] != s[k]) and (s[j] != s[k]):
                    true_cnt += 1
                if (j-i != k-j) and (s[i] != s[j]) and (s[i] != s[k]) and (s[j] != s[k]):
                    true += 1

    print(true)
    print(true_cnt)
    print(s)
"""

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

rl = []
gl = []
bl = []
rld = [0]*(n)
gld = [0]*(n)
bld = [0]*(n)

for i in range(len(s)):
  if s[i] == 'R':
    rl += [i]
    rld[i] = 1 
  elif s[i] == 'G':
    gl += [i]
    gld[i] = 1 
  else:
    bl += [i]
    bld[i] = 1

rlds = rld[:]
glds = gld[:]
blds = bld[:]

for i in range(n-1, 0, -1):
  blds[i-1] += blds[i]
  rlds[i-1] += rlds[i] 
  glds[i-1] += glds[i] 

ans = 0 
for i in rl:
  for j in gl:
    if i >= j or j+1 >= n:
      continue
    ans += blds[j+1]
    if j+j-i < n:
      ans -= bld[j+j-i]

for i in rl:
  for j in bl:
    if i >= j or j+1 >= n:
      continue
    ans += glds[j+1]
    if j+j-i < n:
      ans -= gld[j+j-i]

for i in gl:
  for j in bl:
    if i >= j or j+1 >= n:
      continue
    ans += rlds[j+1]
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in gl:
  for j in rl:
    if i >= j or j+1 >= n:
      continue
    ans += blds[j+1]
    if j+j-i < n:
      ans -= bld[j+j-i]
for i in bl:
  for j in gl:
    if i >= j or j+1 >= n:
      continue
    ans += rlds[j+1]
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in bl:
  for j in rl:
    if i >= j or j+1 >= n:
      continue
    ans += glds[j+1]
    if j+j-i < n:
      ans -= gld[j+j-i]
    
print(ans)

Solution (Python):
def sol(a,b,nu,checker):
    if a==b:
        return None,0
    val={'R':0,'G':1,'B':-1}
    ev={0:'R',1:'G',-1:'B'}
    cal=(val[a]+val[b])*(-1)
    each=ev[cal]
    res=(checker[-1][each]-checker[nu][each])
    return each,res

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




rgb_init={'R':0,'G':0,'B':0}
store=[rgb_init.copy() ]
for i,v in enumerate(s):
    tmp=store[i].copy()
    tmp[v]+=1
    store.append(tmp)

ans=0
for i in range(n):
    for j in range(i+1,n):
        al,val=sol(s[i],s[j],j+1,store)
        if (al and 2*j-i<n)and s[2*j-i]==al:
            val-=1
        ans+=val
print(ans)

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

R = set([v for v,i in enumerate(S) if i == 'R'])
G = set([v for v,i in enumerate(S) if i == 'G'])
B = set([v for v,i in enumerate(S) if i == 'B'])
t = len(B)
c = 0
for i in R:
    for j in G:
        d = [2* i -j,2 * j - i,(i+j)/2]
        for a,s in enumerate(d):
            if s in B:
                c -= 1
        c += t

print(c)

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

L = ['R', 'G', 'B']
accum = [[0] * N for i in range(3)]
for i in range(3):
    l = L[i]
    if S[0] == l:
        accum[i][0] = 1
    for j in range(1, N):
        accum[i][j] = accum[i][j - 1] + (1 if S[j] == l else 0)

ans = 0
for j in range(1, N - 1):
    for i in range(j):
        if S[i] == S[j]:
            continue
        for k in range(3):
            if L[k] != S[i] and L[k] != S[j]:
                lk = L[k]
                ik = k
        ans += accum[ik][N - 1] - accum[ik][j]
        if 2 * (j + 1) - (i + 1) <= N and S[2 * (j + 1) - (i + 1) - 1] == lk:
            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

i=0
rr=[]
for R in s:
    if R=='R':
        rr.append(i)
    i+=1

j=0
gg=[]
for G in s:
    if G=='G':
        gg.append(j)
    j+=1

for rrr in rr:
    for ggg in gg:
        tmp=abs(rrr-ggg)
        if tmp%2==0:
            if max(rrr,ggg)+tmp<n:
                if s[max(rrr,ggg)+tmp]=='B':
                    ans-=1
            if s[(rrr+ggg)//2]=='B':
                ans-=1
            if min(rrr,ggg)-tmp>=0:
                if s[min(rrr,ggg)-tmp]=='B':
                    ans-=1
        else:
            if max(rrr,ggg)+tmp<n:
                if s[max(rrr,ggg)+tmp]=='B':
                    ans-=1
            if min(rrr,ggg)-tmp>=0:
                if s[min(rrr,ggg)-tmp]=='B':
                    ans-=1
print(ans)

Solution (Python):
N = int(input())
S = input()
Rcount = 0
Gcount = 0
Bcount = 0
for i in S:
    if i == "R":
        Rcount += 1
    if i == "G":
        Gcount += 1
    if i == "B":
        Bcount += 1

sum2 = Rcount * Gcount * Bcount

for i in range(1, len(S) // 2 + len(S) % 2):
    for j in range(len(S)-(i*2)):
        
        if S[j] != S[j + i] and S[j] != S[j + (i * 2)] and S[j+i] != S[j+(i*2)]:
            sum2 -= 1
print(sum2)

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

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

ans = c["R"] * c["G"] * c["B"]
target = sorted("RGB")

for i in range(1, N):
    for j in range(N):
        s1,s2,s3 = j,j+i,j+i*2
        if s2 >= N or s3 >= N:
            continue
        t = S[s1] + S[s2] + S[s3]
        if sorted(t) == target:
            ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()
sum = 0
if n < 4:print(0);exit()
r = 0;g = 0; b = 0;
for i in range(len(s)):
    if s[i] == "R":
        r += 1
    if s[i] == "G":
        g += 1
    if s[i] == "B":
        b += 1
sum = r*g*b
for i in range(n):
    for j in range(i+1,n):
        dis = j - i
        k = j + dis
        if k >= n:break

        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i] :
            sum -= 1
print(sum)

Solution (Python):
n=int(input())
S=list(input())
dp=[[0]*(8) for i in range(n+1)]
dp[0][0]=1
for i in range(1,n+1):
  if S[i-1]=="R":
    for j in range(8):
      dp[i][j]=dp[i-1][j]
    dp[i][1]+=dp[i-1][0]
    dp[i][4]+=dp[i-1][2]
    dp[i][6]+=dp[i-1][3]
    dp[i][7]+=dp[i-1][5]
  elif S[i-1]=="G":
    for j in range(8):
      dp[i][j]=dp[i-1][j]
    dp[i][2]+=dp[i-1][0]
    dp[i][4]+=dp[i-1][1]
    dp[i][5]+=dp[i-1][3]
    dp[i][7]+=dp[i-1][6]
  else:
    for j in range(8):
      dp[i][j]=dp[i-1][j]
    dp[i][3]+=dp[i-1][0]
    dp[i][5]+=dp[i-1][2]
    dp[i][6]+=dp[i-1][1]
    dp[i][7]+=dp[i-1][4]
a=0
for i in range(n-2):
  for j in range(i+1,n-1):
    k=j*2-i
    if k>=n:
      continue
    if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
      a=a+1
print(dp[n][7]-a)

Solution (Python):
N = int(input())
S = list(input())
R = []
G = []
B = []
for i in range(len(S)):
    if S[i] == "R":
        R.append(i)
    elif S[i] == "G":
        G.append(i)
    else:
        B.append(i)

ans = len(R) * len(G) * len(B)
for i in range(N):
    for d in range(1, -(-(N-i)//2)):
        if S[i] != S[i + d] and S[i + d] != S[i + 2*d] and S[i + 2*d] != 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')
cnt = 0
for j in range(n-2):
  sj = s[j]
  for i in range(1, (n-j)//2+1):
    if n <= j+2*i:
      break
    sd = s[j+i]
    sd2 = s[j+2*i]
    if (sj != sd) and (sj != sd2) and (sd != sd2):
      cnt += 1
print(r*g*b - cnt)

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

R = [0] * N
G = [0] * N
B = [0] * N

for i in range(1, N):
    if S[-i] == "R":
        R[-i] += R[-i + 1] + 1 if i != 1 else 1
        G[-i] += G[-i + 1]
        B[-i] += B[-i + 1]
    if S[-i] == "G":
        R[-i] += R[-i + 1]
        G[-i] += G[-i + 1] + 1 if i != 1 else 1
        B[-i] += B[-i + 1]
    if S[-i] == "B":
        R[-i] += R[-i + 1]
        G[-i] += G[-i + 1]
        B[-i] += B[-i + 1] + 1 if i != 1 else 1

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        k = j + 1
        if k >= N:
            break
        if S[i] == S[j]:
            continue
        if not ("R" in S[i] + S[j]):
            ans += R[k]
            if R[k] > 0 and (2 * j - i) < N and S[2 * j - i] == "R":
                ans -= 1
        elif not ("G" in S[i] + S[j]):
            ans += G[k]
            if G[k] > 0 and (2 * j - i) < N and S[2 * j - i] == "G":
                ans -= 1
        else:
            ans += B[k]
            if B[k] > 0 and (2 * j - i) < N and S[2 * j - i] == "B":
                ans -= 1
print(ans)

Solution (Python):
def reduce(arr,n):
    count=len(arr)*(len(arr)-1)*(n-len(arr))/2+len(arr)*(len(arr)-1)*(len(arr)-2)/6
    return count

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

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

for i,j in enumerate(s):
    if j=='R':
        r.append(i)
    elif j=='G':
        g.append(i)
    else:
        b.append(i)

count= n*(n-1)*(n-2)/6
count-=reduce(r,n)
count-=reduce(g,n)
count-=reduce(b,n)

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

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

R = [0]*(n+1)
G = [0]*(n+1)
B = [0]*(n+1)

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

ans = 0
for i in range(n):
  for j in range(i+1, n):
    color_selected = [S[i], S[j]]
    color_selected.sort()
    dif = j-i
    if color_selected == ['G', 'R']:
      ans += B[min(j+dif, n)] - B[j+1]
      ans += B[n] - B[min(j+dif+1, n)]
    elif color_selected == ['B', 'R']:
      ans += G[min(j+dif, n)] - G[j+1]
      ans += G[n] - G[min(j+dif+1, n)]
    elif color_selected == ['B', 'G']:
      ans += R[min(j+dif, n)] - R[j+1]
      ans += R[n] - R[min(j+dif+1, n)]

print(ans)