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):
import sys
from collections import *
import heapq
import math
from math import gcd
import bisect
import copy
from itertools import permutations,accumulate,combinations,product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
s=input()
dic={"R":[],"G":[],"B":[]}
for i in range(n):
    if not s[i] in dic:
        dic[s[i]]=[i]
    else:
        dic[s[i]].append(i)

se=set(dic["B"])

ans=len(dic["R"])*len(dic["G"])*len(dic["B"])
for i in range(len(dic["R"])):
    for j in range(len(dic["G"])):
        l,r=min(dic["R"][i],dic["G"][j]),max(dic["R"][i],dic["G"][j])
        if r+(r-l) in se:
            ans-=1
        if l-(r-l) in se:
            ans-=1
        if (r-l)%2==0 and l+(r-l)//2 in se:
            ans-=1
print(ans)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

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

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

  for i,x in enumerate(s):
    if x=='R':
      r[i+1]+=1
    elif x=='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 i,x in enumerate(s):
    for j in range(i+1,n):
      y=s[j]

      k=j+(j-i)
      if k<n:
        
        if x=='R' and y=='G':
          ans+=b[k]-b[j]+b[n]-b[k]
        elif x=='R' and y=='B':
          ans+=g[k]-g[j]+g[n]-g[k]
        elif x=='G' and y=='B':
          ans+=r[k]-r[j]+r[n]-r[k]
        elif x=='G' and y=='R':
          ans+=b[k]-b[j]+b[n]-b[k]
        elif x=='B' and y=='R':
          ans+=g[k]-g[j]+g[n]-g[k]
        elif x=='B' and y=='G':
          ans+=r[k]-r[j]+r[n]-r[k]
        
        z=s[k]
        if j-i==k-j and len(set([x,y,z]))==3:
          ans-=1

      else:
        if x=='R' and y=='G':
          ans+=b[n]-b[j]
        elif x=='R' and y=='B':
          ans+=g[n]-g[j]
        elif x=='G' and y=='B':
          ans+=r[n]-r[j]
        elif x=='G' and y=='R':
          ans+=b[n]-b[j]
        elif x=='B' and y=='R':
          ans+=g[n]-g[j]
        elif x=='B' and y=='G':
          ans+=r[n]-r[j]

  return ans


print(main())

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

idxs = [[] for _ in range(3)]
for i in range(N):
    if S[i] == 'R':
        idxs[0].append(i)
    elif S[i] == 'G':
        idxs[1].append(i)
    else:
        idxs[2].append(i)

import itertools
import bisect

ans = 0
for ch in itertools.permutations(range(3)):
    for idx_a in idxs[ch[0]]:
        for idx_b in idxs[ch[1]]:
            if idx_b < idx_a: continue
            pos_c = bisect.bisect_left(idxs[ch[2]], idx_b)
            pos_ex = bisect.bisect_left(idxs[ch[2]], 2 * idx_b - idx_a)
            
            ans_tmp = len(idxs[ch[2]]) - pos_c
            if pos_ex < len(idxs[ch[2]]) and idxs[ch[2]][pos_ex] == 2 * idx_b - idx_a:
                ans_tmp = max(0, ans_tmp - 1)
            
            ans += ans_tmp

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
        ans += G * B
    elif S[i] == 'G':
        G += 1
        ans += R * B
    elif S[i] == 'B':
        B += 1
        ans += R * G


L = 1
while L * 2 + 1 <= N:
    for i in range(N - L * 2):
        Q = [S[i], S[i + L], S[i + L + L]]
        if len(set(Q)) == 3:
            ans -= 1
    L += 1

print(ans)

Solution (Python):
n = int(input())
s = input()
R = set()
G = set()
B = set()
for i in range(n):
    if s[i] == 'R':
        R.add(i)
    elif s[i] == 'B':
        B.add(i)
    else:
        G.add(i)
ans = 0
for r in R:
    for g in G:
        ans += len(B)
        if 2*r - g in B:
            ans -= 1
        if 2*g - r in B:
            ans -= 1
        if not (r+g)&1 and (r+g)//2 in B:
            ans -= 1
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
s=list(input())
r=[]
g=[]
b=[]
for i in range(n):
    if s[i]=='R':
        r.append(i)
    elif s[i]=='G':
        g.append(i)
    else:
        b.append(i)
ans=len(r)*len(g)*len(b)
r=set(r)
g=set(g)
b=set(b)
for i in r:
    for j in g:
        if (i+j)%2==0 and ((i+j)//2 in b):
            ans-=1
for i in r:
    for j in b:
        if (i+j)%2==0 and ((i+j)//2 in g):
            ans-=1
for i in b:
    for j in g:
        if (i+j)%2==0 and ((i+j)//2 in r):
            ans-=1
print(ans)

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

if n == 1 or n == 2:
    print(0)
    exit()

l = [[0, 0, 0] for i in range(n+1)]
r = [[0, 0, 0] for i in range(n+1)]
for i in range(n):
    if s[i] == 'R':
        l[i+1][0] = l[i][0] +1
        l[i+1][1] = l[i][1]
        l[i+1][2] = l[i][2]
    elif s[i] == 'G':
        l[i+1][0] = l[i][0]
        l[i+1][1] = l[i][1] +1
        l[i+1][2] = l[i][2]
    else:
        l[i+1][0] = l[i][0]
        l[i+1][1] = l[i][1]
        l[i+1][2] = l[i][2] +1

s.reverse()
for i in range(n):
     if s[i] == 'R':
         r[i+1][0] = r[i][0] +1
         r[i+1][1] = r[i][1]
         r[i+1][2] = r[i][2]
     elif s[i] == 'G':
         r[i+1][0] = r[i][0]
         r[i+1][1] = r[i][1] +1
         r[i+1][2] = r[i][2]
     else:
         r[i+1][0] = r[i][0]
         r[i+1][1] = r[i][1]
         r[i+1][2] = r[i][2] +1


r.reverse()


s.reverse()
ans = 0
for i in range(1, n-1):
    if s[i] == 'R':
        ans += l[i][1]*r[i+1][2]
        ans += l[i][2]*r[i+1][1]
    elif s[i] == 'G':
        ans += l[i][0]*r[i+1][2]
        ans += l[i][2]*r[i+1][0]
    else:
        ans += l[i][0]*r[i+1][1]
        ans += l[i][1]*r[i+1][0]
    for j in range(1, i+1):
        if i+j <= n-1:
            if s[i-j] != s[i] and s[i+j] != s[i] and s[i-j] != s[i+j]:
                ans -= 1
print(ans)

Solution (Python):
def read():
    N = int(input())
    S = input()
    return N, S

def solve(N, S, NMAX=4000):
    bucket = [0, 0, 0]
    for i in range(N):
        if S[i] == 'R':
            bucket[0] += 1
        elif S[i] == 'G':
            bucket[1] += 1
        elif S[i] == 'B':
            bucket[2] += 1
    ans = bucket[0] * bucket[1] * bucket[2]
    for i in range(N-2):
        for j in range(i+1, N-1):
            k = j + j - i
            if k >= N:
                continue
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
    return ans

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Solution (Python):
n = int(input())
s = input()
r_num = len([x for x in s if x == 'R'])
g_num = len([x for x in s if x == 'G'])
b_num = n - r_num - g_num
ans = r_num * g_num * b_num
for interval in range(0, (n - 3) // 2 + 1):
    for left_index in range(0, n - (2 * interval + 3) + 1):
        letter0 = s[left_index]
        letter1 = s[left_index + interval + 1]
        letter2 = s[left_index + 2 * interval + 2]
        if len(set([letter0, letter1, letter2])) == 3:
            ans -= 1
print(ans)

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    S = list(input())
    R = set()
    G = set()
    B = set()

    def MIN(a, b):
        if a > b:
            return b
        else:
            return a
    def MAX(a, b):
        if a > b:
            return a
        else:
            return b

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

    b_len = len(B)
    ans = 0
    for r in R:
        for g in G:
            v1 = MIN(r, g)
            v2 = MAX(r, g)
            rg_diff = v2 - v1
            ans += b_len
            if v2 + rg_diff in B:
                ans -= 1
            if v1 - rg_diff in B:
                ans -= 1
            if (r + g) % 2 == 0 and (r + g) // 2 in B:
                ans -= 1
    print(ans)

if __name__ == '__main__':
    main()

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

c = 0

for i in range(N):
    x = 1
    while True:
        j = i + x
        k = j + x
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            c += 1
        x += 1

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

Solution (Python):



def main():
    import sys
    sys.setrecursionlimit(10**6)
    

    def dump(*args):
        sys.stderr.write(str(args))

    n = int(input())
    s = input()
    l = set(list('RGB'))
    cum = [[0]*(n+1) for _ in range(3)]
    for i in range(n):
        ind = -1
        if s[-i - 1] == 'R':
            ind = 0
        elif s[-i - 1] == 'G':
            ind = 1
        elif s[-i - 1] == 'B':
            ind = 2
        for j in range(3):
            cum[j][n - i-1] = cum[j][n - i]
        cum[ind][n - i-1] += 1
    ans = 0
    for i in range(n):
        for j in range(i + 1, n):
            if s[i] == s[j]:
                continue
            last = l.copy()
            last.discard(s[i])
            last.discard(s[j])
            ind = -1
            string = last.pop()
            if string == 'R':
                ind = 0
            elif string == 'G':
                ind = 1
            elif string == 'B':
                ind = 2
            ans += cum[ind][min(j+(j-i)+1, n)]+cum[ind][j+1] - \
                cum[ind][min(j+(j-i), n)]
    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
N = int(input())
S = input()
rs = []
gs = []
bs = []
for i,c in enumerate(S):
    if c=='R':
        rs.append(i)
    elif c=='G':
        gs.append(i)
    else:
        bs.append(i)
R,G,B = len(rs),len(gs),len(bs)
rt = set(rs)
gt = set(gs)
bt = set(bs)

ans = 0
ri = gi = bi = 0
for i in range(N):
    if ri < R and i==rs[ri]:
        ans += gi*(B-bi) + bi*(G-gi)
        for g in gs:
            if i+(i-g) in bt:
                ans -= 1
        ri += 1
    elif gi < G and i==gs[gi]:
        ans += ri*(B-bi) + bi*(R-ri)
        for r in rs:
            if i+(i-r) in bt:
                ans -= 1
        gi += 1
    else:
        ans += ri*(G-gi) + gi*(R-ri)
        for r in rs:
            if i+(i-r) in gt:
                ans -= 1
        bi += 1
print(ans)

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


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
count =[[0 for i in range(3)] for i in range(n+1)]
for i in range(1,n+1):
    if s[i-1] == 'R':
        count[i][0] = count[i-1][0] + 1
    else:
        count[i][0] = count[i-1][0]
    if s[i-1] == 'G':
        count[i][1] = count[i-1][1] + 1
    else:
        count[i][1] = count[i-1][1]
    if s[i-1] == 'B':
        count[i][2] = count[i-1][2] + 1
    else:
        count[i][2] = count[i-1][2]
ans = 0
words = set(['R','G','B'])
for i in range(n):
    for j in range(i+1,n):
        k = set([s[i],s[j]])
        if len(k) == 2:
            remain = (words - k)
            remain = list(remain)[0]
            d = j-i
            if remain == 'R':
                if j + d < n and s[j + d] == 'R':
                    ans += count[-1][0] - count[j][0] - 1
                else:
                    ans += count[-1][0] - count[j][0]
            elif remain == 'G':
                if j + d < n and s[j + d] == 'G':
                    ans += count[-1][1] - count[j][1] - 1
                else:
                    ans += count[-1][1] - count[j][1]
            else:
                if j + d < n and s[j + d] == 'B':
                    ans += count[-1][2] - count[j][2] - 1
                else:
                    ans += count[-1][2] - count[j][2]
print(ans)

Solution (Python):
def main():
    N = int( input())
    S = list( input())
    dp = [[0]*7 for _ in range(N)]
    if S[0] == "R":
        dp[0][0] = 1
    elif S[0] == "G":
        dp[0][1] = 1
    else:
        dp[0][2] = 1
    for i in range(1,N):
        s = S[i]
        for j in range(7):
            dp[i][j] = dp[i-1][j]
        if s == "R":
            dp[i][0] += 1
            dp[i][3] += dp[i-1][1]
            dp[i][5] += dp[i-1][2]
            dp[i][6] += dp[i-1][4]
        elif s == "G":
            dp[i][1] += 1
            dp[i][3] += dp[i-1][0]
            dp[i][4] += dp[i-1][2]
            dp[i][6] += dp[i-1][5]
        else:
            dp[i][2] += 1
            dp[i][4] += dp[i-1][1]
            dp[i][5] += dp[i-1][0]
            dp[i][6] += dp[i-1][3]
    
    ans = dp[N-1][6]
    
    
    
    for d in range(1,N//2+1):
        for i in range(N-d*2):
            
            if S[i] != S[i+d] and S[i] != S[i+2*d] and S[i+d] != S[i+2*d]:
                ans -= 1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
S=input()
r,g,b=[0]*(N+1),[0]*(N+1),[0]*(N+1)
for x,c in ((r,'R'),(g,'G'),(b,'B')):
    for i in range(N):
        x[i+1]=x[i]+(1 if S[i]==c else 0)
a=0
for i in range(1,N-1):
    for j in range(i+1,N):
        for x,c in ((r,'R'),(g,'G'),(b,'B')):
            if S[i-1]!=S[j-1] and c not in (S[i-1],S[j-1]):
                a+=x[N]-x[j]-(x[2*j-i]-x[2*j-i-1] if 2*j-i<N+1 else 0)
print(a)

Solution (Python):


"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()
flush= sys.stdout.flush




def iin(): return int(input())


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





def main():
    from bisect import bisect_right as br
    def find(a, b, c):
        sc = set(c)
        smc = len(c)
        ans = 0
        for i in a:
            for j in b:
                if j>i:
                    x = smc - br(c, j)
                    ans += x - (1 if (j-i)*2+i in sc else 0)
        return ans


    n = iin()
    s = input()
    dc = {'R':[], 'G':[], 'B':[]}
    for i in range(n):
        dc[s[i]].append(i)
    a, b, c = dc['R'], dc['B'], dc['G']
    ans = find(a, b, c) + find(a, c, b) + find(b, a, c) + find(b, c, a) + find(c, a, b) + find(c, b, a)
    print(ans)

main()

Solution (Python):
n=int(input())
s=input()
raccum=[0]*n
gaccum=[0]*n
baccum=[0]*n
for i in range(n):
  if s[i]=='R':
    raccum[i]+=1
  if s[i]=='G':
    gaccum[i]+=1
  if s[i]=='B':
    baccum[i]+=1
for i in range(n-1):
  raccum[i+1]+=raccum[i]
  gaccum[i+1]+=gaccum[i]
  baccum[i+1]+=baccum[i]
ans=0
for i in range(n-2):
  for j in range(i+1,n-1):
    if s[i]=='R':
      if s[j]=='G':
        ans+=baccum[n-1]-baccum[j-1]
        if j+j-i<n and s[j+j-i]=='B':
          ans-=1
      if s[j]=='B':
        ans+=gaccum[n-1]-gaccum[j-1]
        if j+j-i<n and s[j+j-i]=='G':
          ans-=1
    if s[i]=='G':
      if s[j]=='R':
        ans+=baccum[n-1]-baccum[j-1]
        if j+j-i<n and s[j+j-i]=='B':
          ans-=1
      if s[j]=='B':
        ans+=raccum[n-1]-raccum[j-1]
        if j+j-i<n and s[j+j-i]=='R':
          ans-=1
    if s[i]=='B':
      if s[j]=='G':
        ans+=raccum[n-1]-raccum[j-1]
        if j+j-i<n and s[j+j-i]=='R':
          ans-=1
      if s[j]=='R':
        ans+=gaccum[n-1]-gaccum[j-1]
        if j+j-i<n and s[j+j-i]=='G':
          ans-=1
print(ans)

Solution (Python):
def main():
    n=int(input())
    s=list(input())
    ans=0
    how_r=[0 for i in range(n+1)]
    how_g=[0 for i in range(n+1)]
    how_b=[0 for i in range(n+1)]
    for i in range(n):
        string=s[i]
        if string=="R":
            how_r[i+1]+=1
        elif string=="G":
            how_g[i+1]+=1
        elif string=="B":
            how_b[i+1]+=1
    for i in range(n):
        how_r[i+1]+=how_r[i]
        how_g[i+1]+=how_g[i]
        how_b[i+1]+=how_b[i]


    for  i in range(n):
        for j in range(i+1,n):
            x=s[i]
            y=s[j]
            if x!=y:
                if "R" not in [x,y]:
                    ans+=how_r[j+1]-how_r[i]
                    if (i+j)%2==0:
                        if s[(i+j)//2]=="R":
                            ans-=1

                elif "G" not in [x,y]:
                    ans+=how_g[j+1]-how_g[i]
                    if (i+j)%2==0:
                        if s[(i+j)//2]=="G":
                            ans-=1

                elif "B" not in [x,y]:
                    ans+=how_b[j+1]-how_b[i]
                    if (i+j)%2==0:
                        if s[(i+j)//2]=="B":
                            ans-=1  
    print(ans)
if __name__=="__main__":
    main()

Solution (Python):
N = int(input())
S = input()
R = [0]*N
G = [0]*N
B = [0]*N
ans = 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
    elif S[i] == 'G':
        G[i] += 1
    else:
        B[i] += 1
for i in range(N):
    for k in range(i,N):
        if (S[i] != S[k]) and (S[i] != 'R' and S[k] != 'R'):
            ans += R[N-1] - R[k]
            if k+(k-i) < N and S[k+(k-i)] == 'R':
                ans -= 1
        if (S[i] != S[k]) and (S[i] != 'G' and S[k] != 'G'):
            ans += G[N-1] - G[k]
            if k+(k-i) < N and S[k+(k-i)] == 'G':
                ans -= 1
        if (S[i] != S[k]) and (S[i] != 'B' and S[k] != 'B'):
            ans += B[N-1] - B[k]
            if k+(k-i) < N and S[k+(k-i)] == 'B':
                ans -= 1
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = input()

nR = S.count("R")
nG = S.count("G")
nB = S.count("B")
ans = nR*nG*nB
for i in range(N):  
	for j in range(1, N//2+1):  
		if i+2*j >= N:
			continue
		a, b, c = S[i], S[i+j], S[i+2*j]
		if a != b and b != c and c != a:
			ans -= 1
print(ans)

Solution (Python):
from itertools import permutations
n = int(input())
s = input()
s_num = []

for i in range(n):
    num = 0
    if s[i] == 'G':
        num = 1
    elif s[i] == 'B':
        num = 2
    s_num.append(num)




indexs = [[], [], []]
for i, v in enumerate(s_num):
    indexs[v].append(i)

color_sum = [[len(indexs[0])], [len(indexs[1])], [len(indexs[2])]]
for i in range(n):
    for j in range(3):
        if s_num[i] == j:
            color_sum[j].append(color_sum[j][-1] - 1)
        else:
            color_sum[j].append(color_sum[j][-1])

ans = 0
for i, j, k in permutations([0, 1, 2]):
    for first in indexs[i]:
        for second in indexs[j]:
            if second < first:
                continue
            third_exp = second + (second - first)
            if third_exp < n:
                if s_num[third_exp] == k:
                    ans -= 1
            tmp = color_sum[k][second]
            ans += tmp


print(ans)

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

R = [0] * (N + 1)
G = [0] * (N + 1)
B = [0] * (N + 1)
for i, s in enumerate(S):
    R[i + 1] = R[i] + (s == 'R')
    G[i + 1] = G[i] + (s == 'G')
    B[i + 1] = B[i] + (s == 'B')
ans = 0
for i, s in enumerate(S):
    cnt1 = 0
    for j in range(i):
        if (i + i - j) >= N:
            continue
        t = S[j]
        u = S[i + (i - j)]
        if s != t and t != u and u != s:
            cnt1 += 1
    if s == 'R':
        X = G
        Y = B
    elif s == 'G':
        X = R
        Y = B
    else:
        X = R
        Y = G
    cnt2 = X[i] * (Y[N] - Y[i + 1])
    cnt3 = (X[N] - X[i + 1]) * Y[i]
    ans += (cnt2 + cnt3) - cnt1

print(ans)

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

order  = ["R", "G", "B"]
prefix = [[0 for i in range(n+1)] for i in range(3)]

for i in range(n):
	prefix[0][i+1] = prefix[0][i]
	prefix[1][i+1] = prefix[1][i]
	prefix[2][i+1] = prefix[2][i]
	if s[i] == "R":
		prefix[0][i+1] += 1
	elif s[i] == "G":
		prefix[1][i+1] += 1
	elif s[i] == "B":
		prefix[2][i+1] += 1
	else:
		print("Exception!")





ans = 0
for first in range(n):
	for second in range(first+1, n):
		if s[first] == s[second]:
			pass
		else:
			
			
			missing = list({"R", "G", "B"} - {s[first], s[second]})[0]
			mis_order = order.index(missing)

			
			
			to_exclude = second + (second - first)

			ans += prefix[mis_order][n] - prefix[mis_order][second+1]
			if to_exclude < n and s[to_exclude] == missing:
				ans -= 1
			
			
print(ans)

Solution (Python):
n = int(input())
s=input()
r=g=b=0
for index,value in enumerate(s):
    if value=="R":
        r+=1
    elif value=="G":
        g+=1
    else:
        b+=1
ans=r*g*b
for i in range((n-3)//2+1):
    for j in range(n-2-i*2):
        f1=s[j]
        f2=s[j+i+1]
        f3=s[j+2*i+2]
        if f1!=f2!=f3!=f1:
            ans-=1
print(ans)

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

rgb2num = {"R":0, "G":1, "B":2}
num2rgb = {0:"R", 1:"G", 2:"B"}

RGB = [[0, 0, 0] for _ in range(N)]
S = S[::-1]
for i, s in enumerate(S):
    for j in range(3):
        RGB[i][j] = RGB[i - 1][j]
    RGB[i][rgb2num[s]] += 1
RGB = RGB[::-1]
S = S[::-1]



answer = 0
for m in range(1, N - 1):
    for l in range(m - 1, -1, -1):
        
        if S[m] != S[l]:
            
            answer += RGB[m + 1][(3 - rgb2num[S[m]] - rgb2num[S[l]])]
            if 2 * m - l < N and S[2 * m - l] == num2rgb[3 - rgb2num[S[m]] - rgb2num[S[l]]]:
                answer -= 1
print(answer)

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

lr=set([])
lg=set([])
lb=set([])
for i in range(n):
    if s[i]=="R":
        lr.add(i)
    elif s[i]=="G":
        lg.add(i)
    else:
        lb.add(i)

m=0
for i in lr:
    for j in lg:
        if i+(i-j) in lb:
            m+=1
        if j-(i-j) in lb:
            m+=1
        if (i+j)/2 in lb:
            m+=1
print(len(lr)*len(lg)*len(lb)-m)

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

r_is = []
r_dic = {}
g_is = []
g_dic = {}
b_is = []
b_dic = {}

r_num = 0
g_num = 0
b_num = 0
for i, c in enumerate(s):
    if c=="R":
        r_is.append(i)
        
        r_num += 1
    elif c == "G":
        g_is.append(i)
        
        g_num += 1
    else:
        
        b_dic[i] = None
        b_num += 1

out = 0
for ri in r_is:
    for gi in g_is:
        small_i = min(ri, gi) 
        big_i = max(ri, gi) 
        diff = big_i - small_i
        cur_b_num = b_num

        cand_i = small_i - diff
        if cand_i >= 0:
            try:
                b_dic[cand_i]
                cur_b_num -= 1
            except:
                pass

        cand_i = big_i + diff
        if cand_i <= n-1:
            try:
                b_dic[cand_i]
                cur_b_num -= 1
            except:
                pass

        
        wa = small_i + big_i
        if wa % 2 == 0:
            cand_i = wa // 2
            try:
                b_dic[cand_i]
                cur_b_num -= 1
            except:
                pass


        out += cur_b_num

print(out)

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

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


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

print(r * g * b - counter)