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()
ans = s.count('R') * s.count('G') * s.count('B')
for i in range(n):
    for j in range(i, n):
        k = j + (j - i)
        if k >= n:
            break
        if s[i] != s[j] != s[k] != s[i]:
            ans -= 1

print(ans)

Solution (Python):
from collections import Counter

n = int(input())
s = input()
counter = Counter(s)
ans = counter['R'] * counter['G'] * counter['B']
for i in range(n):
    for j in range(i, n):
        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()

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

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

Solution (Python):
n = int(input())
s = input()
R_ls = [0] * n
G_ls = [0] * n
B_ls = [0] * n
R_csum = [0] * n
G_csum = [0] * n
B_csum = [0] * n
for i in range(n):
    if s[i] == 'R':
        R_ls[i] = 1
        R_csum[i] = R_csum[i-1] + 1
        G_csum[i] = G_csum[i-1]
        B_csum[i] = B_csum[i-1]
    elif s[i] == 'G':
        G_ls[i] = 1
        G_csum[i] = G_csum[i-1] + 1
        R_csum[i] = R_csum[i-1]
        B_csum[i] = B_csum[i-1]
    else:
        B_ls[i] = 1
        B_csum[i] = B_csum[i-1] + 1
        R_csum[i] = R_csum[i-1]
        G_csum[i] = G_csum[i-1]
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if (s[i] == 'R' and s[j] == 'G') or (s[i] == 'G' and s[j] == 'R'):
            now = B_csum[-1] - B_csum[j]
            if j + (j-i) < n and s[2*j-i] == 'B':
                now -= 1
        elif (s[i] == 'R' and s[j] == 'B') or (s[i] == 'B' and s[j] == 'R'):
            now = G_csum[-1] - G_csum[j]
            if j + (j-i) < n and s[2*j-i] == 'G':
                now -= 1
        elif (s[i] == 'G' and s[j] == 'B') or (s[i] == 'B' and s[j] == 'G'):
            now = R_csum[-1] - R_csum[j]
            if j + (j-i) < n and s[2*j-i] == 'R':
                now -= 1
        else:
            now = 0
        ans += now
print(ans)

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

count = {"R":0,"G":0,"B":0}
for c in s:
  count[c] += 1

r,g,b = count.values()
ans = r*g*b
for i in range(1,(n-1)//2+1):
  for rgb in zip(s,s[i:],s[i*2:]):
    if len(set(rgb)) == 3:
      ans -=1
print(ans)

Solution (Python):
from copy import deepcopy
n = int(input())
S = input()

accum = [[0,0,0]]
dic = {"R": 0, "G": 1, "B": 2}
accum[0][dic[S[0]]] += 1
for i in range(1, n):
    new_e = deepcopy(accum[i-1])
    new_e[dic[S[i]]] += 1
    accum.append(new_e)
ans = 0
for i in range(n):
    for j in range(i+1, n):
        if S[i] != S[j]:
            if (S[i] == "R" and S[j] == "G") or (S[i] == "G" and S[j] == "R"): 
                ans += accum[n-1][dic["B"]] - accum[j][dic["B"]]
                a = dic["B"]
            elif (S[i] == "R" and S[j] == "B") or (S[i] == "B" and S[j] == "R"):
                ans += accum[n-1][dic["G"]] - accum[j][dic["G"]]
                a = dic["G"]
            else:
                ans += accum[n-1][dic["R"]] - accum[j][dic["R"]]
                a = dic["R"]
            if j + j - i < n and j + j - i > j:
                ans -= accum[j+j-i][a] - accum[j+j-i-1][a]
print(ans)

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

A = [0] * n

for i in range(n):
    if S[i] == "R":
        A[i] = 0
    if S[i] == "G":
        A[i] = 1
    if S[i] == "B":
        A[i] = 2


Cnt = [0] * 3
for a in A:
    Cnt[a] += 1

ans = 1
for cnt in Cnt:
    ans *= cnt


for j in range(n):
    for i in range(j):
        k = 2*j - i
        if k < n:
            if A[i] == A[j]: continue
            if A[i] == A[k]: continue
            if A[k] == A[j]: continue
            ans -= 1
print(ans)

Solution (Python):
import sys
from collections import Counter
input = sys.stdin.readline
n = int(input())
s = list(input())

c  = Counter(s)  

num = c['R']*c['G']*c['B']

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

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

Solution (Python):
N=int(input())
S=input()
R=0;G=1;B=2
C=["R","G","B"]
X=[[[0 for i in range(N+1)]for x in range(2)] for c in range(3)]
for c in range(3):
    for i in range(N):
        if S[i]==C[c]:
            X[c][0][i+1]=X[c][0][i]+1
        else:
            X[c][0][i+1]=X[c][0][i]
    for i in range(N-1,-1,-1):
        if S[i]==C[c]:
            X[c][1][i]=X[c][1][i+1]+1
        else:
            X[c][1][i]=X[c][1][i+1]
ans=0
for i in range(N):
    c=-1
    for cc in range(3):
        if C[cc]==S[i]:
            c=cc
            break
    for seq in [((c+1)%3,(c+2)%3),((c+2)%3,(c+1)%3)]:
        x,y=seq
        ans+=X[x][0][i]*X[y][1][i]
for i in range(N):
    x=0
    while(i+2*x<N):
        j=i+x
        k=i+2*x
        if len({S[i],S[j],S[k]})==3:
            ans-=1
        x+=1
print(ans)

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

import collections
c = collections.Counter(s)

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

for i in range(n-2):
    for j in range(i+1,n-1):
        x = s[i]
        y = s[j]
        
        if x != y:
            if "R" not in [x,y]:
                if 2*j-i < n:
                    if s[2*j-i] == "R":
                        ans -= 1
                    
            elif "G" not in [x,y]:
                if 2*j-i < n:
                    if s[2*j-i] == "G":
                        ans -= 1
                    
            else:
                if 2*j-i < n:
                    if s[2*j-i] == "B":
                        ans -= 1
    
print(ans)

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

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

r = 0
g = 0
b = 0

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

for i in range(n-2):
    for j in range(i+1,n-1):
        x = s[i]
        y = s[j]
        
        if x != y:
            if "R" not in [x,y]:
                ans += R[n-1] - R[j]
                
                if 2*j-i < n:
                    if s[2*j-i] == "R":
                        ans -= 1
                    
            if "G" not in [x,y]:
                ans += G[n-1] - G[j]
                
                if 2*j-i < n:
                    if s[2*j-i] == "G":
                        ans -= 1
                    
            if "B" not in [x,y]:
                ans += B[n-1] - B[j]
                
                if 2*j-i < n:
                    if s[2*j-i] == "B":
                        ans -= 1
    
print(ans)

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

print(ans)

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

sr = [] 
sg = []
sb = [0] * n
kcnt = 0
for i in range(n):
  if s[i] == 'R':
    sr.append(i)
  elif s[i] == 'G':
    sg.append(i)
  else:
    sb[i] = 1
    kcnt += 1

ans = 0
for i in sr:
  for j in sg:
    nki = j*2 - i
    nkj = i*2 - j
    nkk = (j+i)
    ans += kcnt
    if  0<= nki < n and sb[nki] == 1:
        ans -= 1
    if 0<= nkj < n and sb[nkj] == 1:
        ans -= 1
    if nkk%2 == 0 and 0<= (nkk//2) < n and sb[nkk//2] == 1:
      ans -= 1

print(ans)

Solution (Python):


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

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

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

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy






input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
s = input()

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

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

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

print(ans)

Solution (Python):
def resolve():
    '''
    code here
    '''
    import collections
    N = int(input())
    S = input()

    cnt_dict = collections.Counter([item for item in S])
    ng_cnt = 0
    for i in range(N):
        for j in range(i, N):
            k = 2*j - i
            if k < N:
                if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                    ng_cnt += 1
    
    print(cnt_dict['R']*cnt_dict['G']*cnt_dict['B'] - ng_cnt)


if __name__ == "__main__":
    resolve()

Solution (Python):
import sys
import functools
import collections
import operator


def resolve(in_):
    n = int(next(in_))
    s = next(in_).strip()

    c = collections.Counter()
    c[b'R'[0]] = 0
    c[b'G'[0]] = 0
    c[b'B'[0]] = 0
    c.update(s)
    ans = functools.reduce(operator.mul, c.values())
    for i in range(1, n + 1):
        for j in range(i, n + 1):
            k = j * 2 - i
            if k - 1 >= n:
                break
            si = s[i - 1]
            sj = s[j - 1]
            sk = s[k - 1]
            if si != sj and sj != sk and sk != si:
                ans -= 1
    return ans


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

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

Solution (Python):
n=int(input())
s=input()
d={'R':0,'G':0,'B':0}
for i in s:
  d[i]+=1
ans=1
for i in d:
  ans*=d[i]
for k in range(n//2+1):
  for i in range(n-2*k):
    if len(set([s[i],s[i+k],s[i+2*k]]))==3:ans-=1
print(ans)

Solution (Python):

import bisect
from sys import stdin
input = stdin.readline


def main():
  N = int(input())
  S = input()[:-1]

  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)

  Rset = set(R)
  Gset = set(G)
  Bset = set(B)

  sum_ = 0
  for ri in R:
    for gi in G:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(B, gi)
      if idx == len(B): continue
      sum_ += (len(B) - idx)
      if (2*gi - ri) in Bset:
        sum_ -= 1

  for ri in R:
    for gi in B:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(G, gi)
      if idx == len(G): continue
      sum_ += (len(G) - idx)
      if (2*gi - ri) in Gset:
        sum_ -= 1

  for ri in G:
    for gi in R:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(B, gi)
      if idx == len(B): continue
      sum_ += (len(B) - idx)
      if (2*gi - ri) in Bset:
        sum_ -= 1

  for ri in G:
    for gi in B:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(R, gi)
      if idx == len(R): continue
      sum_ += (len(R) - idx)
      if (2*gi - ri) in Rset:
        sum_ -= 1

  for ri in B:
    for gi in R:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(G, gi)
      if idx == len(G): continue
      sum_ += (len(G) - idx)
      if (2*gi - ri) in Gset:
        sum_ -= 1

  for ri in B:
    for gi in G:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(R, gi)
      if idx == len(R): continue
      sum_ += (len(R) - idx)
      if (2*gi - ri) in Rset:
        sum_ -= 1

  print(sum_)


if(__name__ == '__main__'):
  main()

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()

num_r = s.count('R')
num_g = s.count('G')
num_b = s.count('B')

num_ng = 0
for i in range(n):
    for j in range(i+1,n):
        k = j+(j-i)

        if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            num_ng += 1

print(num_r*num_g*num_b-num_ng)

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

def get_ans(ans_,c):
    if c < 0:
        pass
    else:
        ans_ -= B_[c]
    return ans_




S_dict={'R':set(),'G':set(),'B':set()}
for s_idx,s in enumerate(S):
    S_dict[s].add(s_idx+1)
    

B_ = [0]*(2*N)
for s_b in S_dict['B']:
    B_[s_b]=1
    
S_R=S_dict['R']
S_G=S_dict['G']
    
ans=0
ans__=len(S_dict['B'])
for s_r in S_R:
    for s_g in S_G:
        ans_=ans__
        c_= abs(s_r-s_g)
        c_mean = 0
        if (s_r + s_g)%2 == 0:
            c_mean = (s_r + s_g)//2
            ans_ = get_ans(ans_,c_mean)
            


        
        if s_r > s_g:
            ans_ = get_ans(ans_,s_r + c_)
            ans_ = get_ans(ans_,s_g - c_)
            

            
        else:
            ans_=get_ans(ans_,s_r - c_)
            ans_=get_ans(ans_,s_g + c_)
            

        
        ans+=ans_
        

        
print(ans)

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

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

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

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


def check(s1, s2, s3):
    t = 0
    for i in range(1, n+1):
        if not i in s1:
            continue
        for j in range(i+1, n+1):
            if not j in s2:
                continue
            if (j+(j-i)) in s3:
                t += 1
    return t


ans -= check(r, g, b)
ans -= check(r, b, g)
ans -= check(g, b, r)
ans -= check(g, r, b)
ans -= check(b, r, g)
ans -= check(b, g, r)
print(ans)

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

freq = {'R': 0, 'G': 0, 'B': 0}
for x in s:
    freq[x] += 1

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

print(ans)

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

Solution (Python):
n=int(input())
s=input()
ans=0
for i in range(n-2):
    if s[i]=="R":
        a,b="G","B"
    elif s[i]=="G":
        a,b="B","R"
    else:
        a,b="R","G"
    cnt=[None]*(n-i-1)
    acnt=0
    bcnt=0
    for j in range(i+1,n):
        if s[j]==a:
            acnt+=1
            cnt[j-i-1]=a
        elif s[j]==b:
            bcnt+=1
            cnt[j-i-1]=b
    tmp=acnt*bcnt
    for j in range((n-i-1)//2):
        if cnt[j]==a and cnt[(j+1)*2-1]==b:
            tmp-=1
        elif cnt[j]==b and cnt[(j+1)*2-1]==a:
            tmp-=1
    ans+=tmp
print(ans)

Solution (Python):
N = int(input())
S = input()
d = {
    'R': {},
    'G': {},
    'B': {}
}
for i, s in enumerate(S):
    d[s][i] = True
answer = len(d['R']) * len(d['G']) * len(d['B'])
for r in d['R'].keys():
    for g in d['G'].keys():
        if 2 * r - g in d['B']:
            answer -= 1
        if 2 * g - r in d['B']:
            answer -= 1
        if ((r + g) % 2 == 0 and ((r + g) // 2) in d['B']):
            answer -= 1
            

print(answer)

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

score = 0

for i in range(1,n-1):
    if s[i] == 'R':

        g = 0
        g_l = []
        for j in range(i):
            if s[j] == 'G':
                g += 1
                g_l.append(i-j)
        b = 0         
        b_l = []
        for j in range(i,n):
            if s[j] == 'B':
                b += 1
                b_l.append(j-i)
                
        common = set(g_l) & set(b_l)
        score += b*g - len(common)

for i in range(1,n-1):
    if s[i] == 'R':

        g = 0
        g_l = []
        for j in range(i):
            if s[j] == 'B':
                g += 1
                g_l.append(i-j)
        b = 0         
        b_l = []
        for j in range(i,n):
            if s[j] == 'G':
                b += 1
                b_l.append(j-i)
                
        common = set(g_l) & set(b_l)
        score += b*g - len(common)

for i in range(1,n-1):
    if s[i] == 'G':

        g = 0
        g_l = []
        for j in range(i):
            if s[j] == 'R':
                g += 1
                g_l.append(i-j)
        b = 0         
        b_l = []
        for j in range(i,n):
            if s[j] == 'B':
                b += 1
                b_l.append(j-i)
                
        common = set(g_l) & set(b_l)
        score += b*g - len(common)

for i in range(1,n-1):
    if s[i] == 'G':

        g = 0
        g_l = []
        for j in range(i):
            if s[j] == 'B':
                g += 1
                g_l.append(i-j)
        b = 0         
        b_l = []
        for j in range(i,n):
            if s[j] == 'R':
                b += 1
                b_l.append(j-i)
                
        common = set(g_l) & set(b_l)
        score += b*g - len(common)

for i in range(1,n-1):
    if s[i] == 'B':

        g = 0
        g_l = []
        for j in range(i):
            if s[j] == 'R':
                g += 1
                g_l.append(i-j)
        b = 0         
        b_l = []
        for j in range(i,n):
            if s[j] == 'G':
                b += 1
                b_l.append(j-i)
                
        common = set(g_l) & set(b_l)
        score += b*g - len(common)

for i in range(1,n-1):
    if s[i] == 'B':

        g = 0
        g_l = []
        for j in range(i):
            if s[j] == 'G':
                g += 1
                g_l.append(i-j)
        b = 0         
        b_l = []
        for j in range(i,n):
            if s[j] == 'R':
                b += 1
                b_l.append(j-i)
                
        common = set(g_l) & set(b_l)
        score += b*g - len(common)
        
print(score)

Solution (Python):
from collections import Counter

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

C=Counter(S)
r=C['R']
g=C['G']
b=C['B']

ans=r*g*b


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

print(ans)

Solution (Python):

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

count_R = 0
count_G = 0
count_B = 0
for s in S:
  if s == "R":
    count_R += 1
  elif s == "G":
    count_G += 1
  else:
    count_B += 1

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

print(count_R * count_G * count_B - count)

Solution (Python):

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

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

cnt = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = 2 * j - i
        if k >= N:
            continue

        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            cnt += 1

print(d["R"] * d["G"] * d["B"] - cnt)

Solution (Python):

from bisect import bisect_left

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

d = {"R": [], "G": [], "B": []}
for i in range(N):
    d[S[i]].append(i)


def find(x, y, z):
    total = len(z)
    res = 0
    for i in x:
        for j in y:
            if i > j:
                continue

            
            k = bisect_left(z, j)

            
            k0 = bisect_left(z, 2 * j - i)
            flag = int(k0 >= k and k0 < total and z[k0] == 2 * j - i)
            res += total - k - flag

    return res


ans = 0
ans += find(d["R"], d["G"], d["B"])
ans += find(d["R"], d["B"], d["G"])
ans += find(d["G"], d["R"], d["B"])
ans += find(d["G"], d["B"], d["R"])
ans += find(d["B"], d["R"], d["G"])
ans += find(d["B"], d["G"], d["R"])
print(ans)

Solution (Python):
from collections import Counter
import itertools
n=int(input())
rgb=list(input())
rgb_counter=Counter(rgb)
n_list=[i for i in range(1,n+1)]
count=0
for i,j in itertools.combinations(n_list,2):
    k=2*j-i
    if k<=n and rgb[j-1]!=rgb[i-1] and rgb[j-1]!=rgb[k-1] and rgb[i-1]!=rgb[k-1]:
        count+=1
print(rgb_counter["R"]*rgb_counter["G"]*rgb_counter["B"]-count)

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 and S[i]!=S[j]!=S[k]!=S[i]:
      ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()
r,g,b = [0]*(n+1),[0]*(n+1),[0]*(n+1)
for i in range(1,n+1):
    if s[i-1]=='R':
        r[i] =r[i-1]+1
        g[i] = g[i-1]
        b[i] = b[i-1]
    elif s[i-1] =='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 i in range(1,n+1):
    for j in range(i+1,n+1):
        p = ['R','G','B']
        if s[i-1] ==s[j-1]:
            continue
        else:
            p.remove(s[i-1])
            p.remove(s[j-1])
            if 2*j-i-1 <n and p[0] ==s[2*j-i-1]:
                ans -=1
            if p[0]=='R':
                ans += r[-1]-r[j]
            elif p[0]=='B':
                ans += b[-1]-b[j]
            else:
                ans += g[-1]-g[j]
print(ans)

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

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

Solution (Python):
from collections import Counter
N = int(input())
S = list(input())
cnt = Counter(S)
Red, Green, Blue = (cnt['R'], cnt['G'], cnt['B'])
patn = Red*Green*Blue

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

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

r = 0
g = 0
b = 0
for i in s:
  if i == 'R':
    r += 1
  elif i == 'G':
    g += 1
  else:
    b += 1

all = r*g*b
ho = 0
for i in range(0,n):
  for j in range(i+1,n):
    if s[i] == s[j]:
      continue
    k = 2*j-i
    if k >= n:
      continue
    if s[i] == s[k] or s[j] == s[k]:
      continue
    ho += 1
print(all-ho)

Solution (Python):



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

    
    

    
    r_count = s.count('R')
    g_count = s.count('G')
    b_count = s.count('B')

    ans = r_count * g_count * b_count

    
    
    for j in range(n):
        for i in range(j):
            
            k = 2 * j - i

            
            if k < n:
                if s[i] == s[j]:
                    continue
                elif s[i] == s[k]:
                    continue
                elif s[j] == s[k]:
                    continue

                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

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

total = r*g*b

for i in range(0,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]:
            total-=1


print(total)

Solution (Python):



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

    
    

    
    r_count = s.count('R')
    g_count = s.count('G')
    b_count = s.count('B')

    ans = r_count * g_count * b_count

    
    
    for j in range(n):
        for i in range(j):
            
            k = 2 * j - i

            
            if k < n:
                if s[i] == s[j]:
                    continue
                elif s[i] == s[k]:
                    continue
                elif s[j] == s[k]:
                    continue

                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n = k()
s = s()
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((n-i+1)//2):
        if s[i]!=s[i+j] and s[i+j]!=s[i+2*j] and s[i+2*j]!=s[i]:
            ans-=1

print(ans)

Solution (Python):
from collections import Counter

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

cnt = Counter(s)

rn=cnt['R']
gn=cnt['G']
bn=n-rn-gn

ans=bn*gn*rn
if n>2:
  
  for x in range(n-2):
    
    y=(n-1-x)//2
    for i in range(1,y+1):
      if  (not s[x]==s[x+i]) & (not s[x]==s[x+i+i]) & (not s[x+i]==s[x+i+i]):
        ans-=1
        
  print(ans)
    
else:
  print(0)

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

rn=s.count('R')
gn=s.count('G')
bn=n-rn-gn

ans=bn*gn*rn
if n>2:
  
  for x in range(n-2):
    
    y=(n-1-x)//2
    for i in range(1,y+1):
      if  (not s[x]==s[x+i]) & (not s[x]==s[x+i+i]) & (not s[x+i]==s[x+i+i]):
        ans-=1
        
  print(ans)
    
else:
  print(0)

Solution (Python):
import sys
from collections import Counter

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
s = input().rstrip()

cnt = Counter(s)
R = cnt['R']
G = cnt['G']
B = cnt['B']

cnt = 0
for i in range(n):
    for j in range(1, n):
        if n <= i + j * 2:
            break
        if s[i] != s[i + j] and s[i + j] != s[i + 2 * j] and s[i] != s[i + 2 * j]:
            cnt += 1

print(R*G*B-cnt)

Solution (Python):
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    S = readline().strip()

    counter = Counter(S)
    ans = counter['R'] * counter['G'] * counter['B']

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    S = readline().strip()

    counter = Counter(S)
    ans = counter['R'] * counter['G'] * counter['B']
    
    for w in range(1, (N - 1) // 2 + 1):
        for i in range(N - 2 * w):
            if S[i] != S[i + w] and S[i] != S[i + 2 * w] and S[i + w] != S[i + 2 * w]:
                ans -= 1

    print(ans)
    return


if __name__ == '__main__':
    main()

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 d in range(N):
        j = i+d
        k = i+2*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):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    N = I()
    s = S()

    R = s.count("R")
    G = s.count("G")
    B = s.count("B")

    dup = 0
    for i in range(N):
        for k in range(i+1, N):
            if (i + k) % 2 == 0:
                j = (i + k) // 2
                if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                    dup += 1

    print(R * G * B - dup)

main()

Solution (Python):
from collections import Counter

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

counter = Counter(list(S))

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

for d in range(1, N // 2 + 1):
    for a, b, c in zip(S, S[d:], S[2 * d:]):
        if a != b and b != c and c != a:
            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 - 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):
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 - 1 and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1

print(ans)