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):
from collections import Counter
N = int(input())
S = input()

rgb_cnt = Counter(S)
ans = rgb_cnt["R"] * rgb_cnt["G"] * rgb_cnt["B"] 

for i in range(1,N-1):
    max_dist = min(i,N-i-1)
    m = S[i]
    for d in range(1,max_dist+1): 
        l = S[i-d]
        r = S[i+d]
        if (l!=m and r!=m and l!= r):
            ans -= 1
print(ans)

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

from collections import defaultdict
d = defaultdict(list)

for i,s in enumerate(S):
    d[s].append(i)

Bs = [0] * 8000
cnt_b = len(d["B"])

for i in  d["B"]:
    Bs[i] += 1

ans = 0
for i_r in d["R"]:
    for i_g in d["G"]:
        ls = []
        mx = max(i_r,i_g)
        mn = min(i_r,i_g)
        if (mx-mn) % 2 == 0:
            ls.append((mx+mn)//2)    
        ls.append(mx + (mx-mn))
        if  mn - (mx-mn) >= 0: 
            ls.append(mn-(mx-mn)) 
        cnt = 0
        for l in ls:
            cnt += Bs[l]
        
        ans += cnt_b - cnt
print(ans)

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

    r_sums = [0] * N
    g_sums = [0] * N
    b_sums = [0] * N

    for i in range(N):
        r = 0
        g = 0
        b = 0
        if S[i] == 'R':
            r = 1
        if S[i] == 'G':
            g = 1
        if S[i] == 'B':
            b = 1
        
        if i == 0:
            r_sums[0] = r
            g_sums[0] = g
            b_sums[0] = b
        else:
            r_sums[i] = r_sums[i-1] + r
            g_sums[i] = g_sums[i-1] + g
            b_sums[i] = b_sums[i-1] + b

    ans = 0
    for i in range(N-2):
        for j in range(i,N-1):
            st = set([S[i],S[j]])
            if len(st) == 1:
                continue
            k_color = ""
            for color in ["R","G","B"]:
                if not color in st:
                    k_color = color
                    break
            
            sub = 0
            if 0 <= j+(j-i) < N:
                if S[j+(j-i)] == k_color:
                    sub = 1

            if k_color == "R":
                ans += r_sums[-1] - r_sums[j] - sub
            elif k_color == "G":
                ans += g_sums[-1] - g_sums[j] - sub
            elif k_color == "B":
                ans += b_sums[-1] - b_sums[j] - sub
    
    print(ans)

if __name__ == '__main__':
    solve()

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

count=0
count_r=0
count_g=0
count_b=0

for s in S:
    if s=="G":
        count_g+=1
    elif s=="R":
        count_r+=1
    else:
        count_b+=1

for i in range((N-3)//2+1):
    
    for j in range(N-(i*2)-2):
        
        if S[j]==S[j+i+1] or S[j]==S[j+2*i+2] or S[j+i+1]==S[j+2*i+2]:
            pass
        else:
            count+=1

answer=count_g*count_r*count_b-count

print(answer)

Solution (Python):
import sys
from itertools import permutations as perm
from bisect import bisect_left as bisect
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


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


def gcd(a, b):
    
    while b > 0:
        a, b = b, a % b

    return a


def solve():
    def get_ans(c1, c2, c3):
        ret = 0
        lenc3 = len(RGB[c3])
        for c1i in range(len(RGB[c1])):
            tmp = bisect(RGB[c2], RGB[c1][c1i])
            for c2i in range(tmp, len(RGB[c2])):
                c3i = bisect(RGB[c3], RGB[c2][c2i])
                if lenc3 == c3i:
                    continue
                
                c21d = RGB[c2][c2i] - RGB[c1][c1i]
                t = bisect(RGB[c3], RGB[c2][c2i] + c21d, c3i)

                if lenc3 != t and RGB[c3][t] - RGB[c2][c2i] == c21d:
                    ret += lenc3 - c3i - 1
                else:
                    ret += lenc3 - c3i

        return ret
    int(input())
    S = list(input().strip())
    RGB = {k: [] for k in "RGB"}
    for i, s in enumerate(S):
        RGB[s].append(i)

    ans = 0
    
    for c1, c2, c3 in perm(list("RGB")):
        
        ans += get_ans(c1, c2, c3)
        
    print(ans)


solve()

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

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

d = 0

for p in range(0,n-2):
    q = 1 
    while p+2*q<=n-1:
        if s[p] != s[p+q] and s[p] != s[p+2*q] and s[p+q] != s[p+2*q]:
            d+=1
            
            q += 1
            
            
        else:
            
            q = q+1

print(r*g*b-d)

Solution (Python):
import numpy as np


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


S_arr = np.array(list(S))

R_idx = np.where(S_arr == 'R')[0]
G_idx = np.where(S_arr == 'G')[0]
B_idx = np.where(S_arr == 'B')[0]

ans_cand = R_idx.shape[0] * G_idx.shape[0] * B_idx.shape[0]


for i in range(N-2):
    first = S[i]
    for j in range(i+1, N-1):
        second = S[j]
        sa = j - i
        k = j + sa
        if first == second:
            continue
        elif k >= N:
            break
        elif S[k] != first and S[k] != second:
            ans_cand -= 1

print(ans_cand)

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

sameRangeRGBs = [0] * N
for itvl in range(1, (N-1)//2 + 1):
    for i in range(itvl, N - itvl):
        l = S[i-itvl]
        m = S[i]
        r = S[i+itvl]
        if l == m or m == r or r == l:
            continue
        sameRangeRGBs[i] += 1

accRL = [0]
accGL = [0]
accBL = [0]
numR = 0
numG = 0
numB = 0
for c in S:
    if c == 'R':
        numR += 1
    elif c == 'G':
        numG += 1
    elif c == 'B':
        numB += 1
    accRL.append(numR)
    accGL.append(numG)
    accBL.append(numB)

accRR = [0]
accGR = [0]
accBR = [0]
numR = 0
numG = 0
numB = 0
for c in S[::-1]:
    if c == 'R':
        numR += 1
    elif c == 'G':
        numG += 1
    elif c == 'B':
        numB += 1
    accRR.append(numR)
    accGR.append(numG)
    accBR.append(numB)

ans = 0
for i in range(N):
    c = S[i]
    if c == 'R':
        ans += accGL[i] * accBR[N-1-i] + accBL[i] * accGR[N-1-i] - sameRangeRGBs[i]
    elif c == 'G':
        ans += accRL[i] * accBR[N-1-i] + accBL[i] * accRR[N-1-i] - sameRangeRGBs[i]
    elif c == 'B':
        ans += accGL[i] * accRR[N-1-i] + accRL[i] * accGR[N-1-i] - sameRangeRGBs[i]
print(ans)

Solution (Python):
from itertools import accumulate
n = int(input())
S = input().replace("R", "1").replace("G", "2").replace("B", "3")
RGB = [[0]*n for _ in range(4)]
for i, s in enumerate(S):
    RGB[int(s)][i] = 1
for i in range(4):
    RGB[i] = list(accumulate(RGB[i]))
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        t = int(S[i]) ^ int(S[j])
        ans += RGB[t][-1] - RGB[t][j]
        if 2*j-i < n and int(S[2*j-i]) == t:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
words = [[0]*3for _ in range(n)] 
for i in range(n-2,-1,-1):
    if s[i+1] == 'R':
        words[i][0]+=1
    elif s[i+1] == 'G':
        words[i][1] +=1
    else:words[i][2] +=1
    words[i][0] +=words[i+1][0]
    words[i][1] +=words[i+1][1]
    words[i][2] +=words[i+1][2]
ans = 0
for i in range(n):
    for j in range(i+1,n):
        
        if s[i] == 'R':
            if s[j] == 'G':
                ans += words[j][2]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'B':ans-=1
            if s[j] == 'B':
                ans += words[j][1]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'G':ans-=1
        if s[i] == 'G':
            if s[j] == 'R':
                ans += words[j][2]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'B':ans-=1
            if s[j] == 'B':
                ans += words[j][0]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'R':ans-=1
        if s[i] == 'B':
            if s[j] == 'G':
                ans += words[j][0]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'R':ans-=1
            if s[j] == 'R':
                ans += words[j][1]
                if j+j-i >n-1:continue
                if s[j+(j-i)] == 'G':ans-=1
print(ans)

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


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

ss = s[::-1]
for i in range(n):
    c = ss[i]
    
    
    
    

    r[i + 1] = r[i] + (c == 'R')
    g[i + 1] = g[i] + (c == 'G')
    b[i + 1] = b[i] + (c == 'B')

    
    
    
    
    
    

r = r[::-1]
g = g[::-1]
b = b[::-1]

ans = 0




for i in range(n):
    if s[i] != 'R':
        continue
    for j in range(i + 1, n):
        if s[j] != 'G':
            continue
        d = j - i
        ans += b[j]
        if j + d < n and s[j + d] == 'B':
            ans -= 1

for i in range(n):
    if s[i] != 'R':
        continue
    for j in range(i + 1, n):
        if s[j] != 'B':
            continue
        d = j - i
        ans += g[j]
        if j + d < n and s[j + d] == 'G':
            ans -= 1

for i in range(n):
    if s[i] != 'G':
        continue
    for j in range(i + 1, n):
        if s[j] != 'R':
            continue
        d = j - i
        ans += b[j]
        if j + d < n and s[j + d] == 'B':
            ans -= 1

for i in range(n):
    if s[i] != 'G':
        continue
    for j in range(i + 1, n):
        if s[j] != 'B':
            continue
        d = j - i
        ans += r[j]
        if j + d < n and s[j + d] == 'R':
            ans -= 1

for i in range(n):
    if s[i] != 'B':
        continue
    for j in range(i + 1, n):
        if s[j] != 'R':
            continue
        d = j - i
        ans += g[j]
        if j + d < n and s[j + d] == 'G':
            ans -= 1

for i in range(n):
    if s[i] != 'B':
        continue
    for j in range(i + 1, n):
        if s[j] != 'G':
            continue
        d = j - i
        ans += r[j]
        if j + d < n and s[j + d] == 'R':
            ans -= 1

print(ans)

Solution (Python):
from collections import Counter


def main(N=None, S=None):
    if not N or not S:
        N = int(input())
        S = input()
    c = Counter(S)
    ans = c["R"]*c["G"]*c["B"]
    for hop in range(1, N):
        for start in range(N):
            i = start
            j = start + hop
            k = start + hop*2
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                ans -= 1
            
    print(ans)
    return ans


if __name__ == "__main__":
    
    
    main()

Solution (Python):
import bisect

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

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

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

blen = len(b)
for rr in r:
    for gg in g:
        nb = (rr + gg) / 2
        i = bisect.bisect_left(b, nb)
        if int(nb) == nb and i < blen and b[i] == nb:
            count -= 1

        nb = 2 * rr - gg
        i = bisect.bisect_left(b, nb)
        if int(nb) == nb and i < blen and b[i] == nb:
            count -= 1

        nb = 2 * gg - rr
        i = bisect.bisect_left(b, nb)
        if int(nb) == nb and i < blen and b[i] == nb:
            count -= 1

print(count)

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

N = int(input())
S = list(input())
R = []
G = []
B = set()
for i in range(N):
    if S[i] =="R":
        R.append(i)
    if S[i] =="G":
        G.append(i)
    if S[i] =="B":
        B.add(i)
M =len(B)
cnt = 0
for r in R:
    for g in G:
        a = (r+g)/2
        b = 2*g-r
        c = 2*r-g
        if a in B:
            cnt-=1
        if b in B:
            cnt-=1
        if c in B:
            cnt-=1
        cnt += M
        
print(cnt)

Solution (Python):
import collections
N = int(input())
S = input()
c = collections.Counter([s for s in S])
RGB = c.most_common()
if len(RGB) != 3:
  result = 0
else:
  rgb = set(["R", "G", "B"])
  result = RGB[0][1] * RGB[1][1] * RGB[2][1]
  for i in range(1, N//2+1):
    for j in range(N-i*2):
      if set([S[j], S[j+i], S[j+i*2]]) == rgb:
        result -= 1
print(result)

Solution (Python):
N = int(input())
S = str(input())
R, G, B = 0, 0, 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(1, 2000):
  for j in range(1, N - 2 * i + 1):
    if S[j - 1] != S[j + i - 1]:
      if S[j + 2 * i - 1] != S[j + i - 1]:
        if S[j - 1] != S[j + 2 * i - 1]:
          ans -= 1
            
print(max(ans, 0))

Solution (Python):

"""
Created on Sun Apr 12 20:58:46 2020

@author: Kanaru Sato
"""

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

rdic = {}
gdic = {}
bdic = {}

for i in range(len(s)):
    if s[i] == "R":
        if i not in rdic:
            rdic[i] = 1
    elif s[i] == "G":
        if i not in gdic:
            gdic[i] = 1
    elif s[i] == "B":
        if i not in bdic:
            bdic[i] = 1

bnum = len(bdic)            
count = 0
for rplace in rdic:
    for gplace in gdic:
        if rplace >= gplace:
            ex1 = gplace - (rplace- gplace)
            ex2 = rplace + (rplace- gplace)
            ex3 = (rplace + gplace)/2
            e1 = 1 if ex1 in bdic else 0
            e2 = 1 if ex2 in bdic else 0
            e3 = 1 if ex3 in bdic else 0
            count += bnum - (e1 + e2 + e3)
        elif rplace < gplace:
            ex1 = rplace - (gplace- rplace)
            ex2 = gplace + (gplace- rplace)
            ex3 = (rplace + gplace)/2
            e1 = 1 if ex1 in bdic else 0
            e2 = 1 if ex2 in bdic else 0
            e3 = 1 if ex3 in bdic else 0
            count += bnum - (e1 + e2 + e3)

print(count)

Solution (Python):
from collections import Counter
from operator import mul
n = int(input())
s = input()
c = Counter(s)
if n < 3:
    print(0)
    exit()
elif len(c.keys()) < 3:
    print(0)
    exit()
ans = 0
for i in range(n):
    x = s[i]
    ans += mul(*[v for k, v in c.items()if k != x])
    c[x] -= 1
    for j in range(i+1, n):
        y = s[j]
        if x == y:
            continue
        idx_diff = j-i
        if n <= j+idx_diff:
            continue
        z = s[j+idx_diff]
        if x != z and y != z:
            ans -= 1
print(ans)

Solution (Python):
from collections import Counter
n = int(input())
s = input()
slen = len(s)
d = Counter(s)
res = d['R']*d['G']*d['B']

for start in range(slen):
    for step in range(1, slen):
        i = start
        j = i + step
        k = j + step
        if k >= slen:
            break
        ijk = [s[i], s[j], s[k]]
        
        if 'R' in ijk and 'G' in ijk and 'B' in ijk:
            res -= 1

print(res)

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

def count(index,Set):
    if index in Set:
        return 1
    else:
        return 0

L=[[],[],set()]
for x in range(N):
    if S[x]=='R':
        L[0].append(x)
    elif S[x]=='G':
        L[1].append(x)
    else:
        L[2].add(x)

number=len(L[2])

ans=0
for i in L[0]:
    for j in L[1]:
        if (i+j)%2==0:
            m=(i+j)//2
            res1=number-count(m,L[2])-count(2*i-j,L[2])-count(2*j-i,L[2])
            ans+=max(0,res1)
        else:
            res2=number-count(2*i-j,L[2])-count(2*j-i,L[2])
            ans+=max(0,res2)

print(ans)

Solution (Python):
N = int(input())
S = list(input())
R = []
G = []
B = []

def check(startI, list1, list2, char1, char2):
  num1, num2 = 0, 0
  sub_ = 0
  for num in list1:
    if num > startI:
      if num + (num-startI) < N and S[num+(num-startI)] == char2:
        sub_ += 1
      num1 += 1
  for num in list2:
    if num > startI:
      if num + (num-startI) < N and S[num+(num-startI)] == char1:
        sub_ += 1
      num2 += 1
  return num1 * num2 - sub_


for i in range(N):
  if S[i] == 'R':
    R.append(i)
  elif S[i] == 'G':
    G.append(i)
  elif S[i] == 'B':
    B.append(i)

sum_ = 0
for i in range(N):
  if S[i] == 'R':
    sum_ += check(i, G, B, 'G', 'B')
  elif S[i] == 'G':
    sum_ += check(i, R, B, 'R', 'B')
  elif S[i] == 'B':
    sum_ += check(i, R, G, 'R', 'G')

print(sum_)

Solution (Python):

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


r_list = [0] * n
g_list = [0] * n
b_list = [0] * n
s = s[::-1]
for i,v in enumerate(s):
    if v == "R":
        r_list[i]=1
    elif v=="G":
        g_list[i]=1
    else:
        b_list[i]=1




from itertools import accumulate

r_list = list(accumulate(r_list))
b_list = list(accumulate(b_list))
g_list = list(accumulate(g_list))

r_list.reverse()
g_list.reverse()
b_list.reverse()
s = s[::-1]

ret = 0
for i,v in enumerate(s[0:n-1]):
    if v=="R":
        ret+=(g_list[i+1] * b_list[i+1])
    elif v=="G":
        ret+=(r_list[i+1] * b_list[i+1])
    else:
        ret+=(r_list[i+1] * g_list[i+1])
    step = 0
    while True:
        step+=1
        if i+step*2 >= n:
            break
        if v!=s[i+step] and v!= s[i+step*2] and s[i+step]!=s[i+step*2]:
            ret-=1

print(ret)

Solution (Python):

n = int(input())
s = input()
ls_r = []
ls_g = []
ls_b = []
len_r = 0
len_g = 0
len_b = 0

if n <=3:
    print(0)
else:
    ans = 0
    for i in range(n):
        if s[i] == "R":
            ls_r.append(i)
            len_r += 1
        elif s[i] == "G":
            ls_g.append(i)
            len_g += 1
        else:
            ls_b.append(i)
            len_b += 1
    for b in ls_b:
        for g in ls_g:
            tmp = 0
            if b < g:
                if 2 * b - g >= 0:
                    if s[2*b-g] == "R":
                        tmp -= 1
                if 2 * g - b <= n-1:
                    if s[2*g-b] == "R":
                        tmp -= 1
                if (g - b) % 2 == 0:
                    if s[(g+b)//2] == "R":
                        tmp -= 1
                ans += tmp+len_r
            else:
                if 2 * g - b >= 0:
                    if s[2*g-b] == "R":
                        tmp -= 1
                if 2 * b - g <= n-1:
                    if s[2*b-g] == "R":
                        tmp -= 1
                if (b - g) % 2 == 0:
                    if s[(g+b)//2] == "R":
                        tmp -= 1
                ans += tmp+len_r
    print(ans)

Solution (Python):
def rgb(s):
    if s=="R":return 0
    if s=="G":return 1
    if s=="B":return 2
def rgb2(s,t):
    if (s=="R" and t =="G" ) or (t=="R" and s =="G" ):
        return 2
    elif (s=="R" and t =="B" ) or (s=="B" and t =="R" ):
        return 1
    else:return 0


n = int(input())
s = input()
co = [[0]*3 for i in range(n+1)]
for i in range(1,n+1):
    for j in range(3):
        co[i][j] = co[i-1][j]
    co[i][rgb(s[i-1])] = co[i-1][rgb(s[i-1])] + 1
ans = 0
for i in range(n):
    w1 = s[i]
    for j in range(i+1,n):
        w2 = s[j]
        d = j-i
        if w1!=w2:
            ans += co[-1][rgb2(w1,w2)] - co[j][rgb2(w1,w2)]
            if j+d <= n-1:
                if rgb(s[j+d]) == rgb2(w1,w2):
                    ans-=1
print(ans)

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

r_count=0
g_count=0
b_count=0

ans = 0

for i in range(n):
    if s[i]=="R":
        r_count+=1
    if s[i]=="G":
        g_count+=1
    if s[i]=="B":
        b_count+=1
ans+=r_count*g_count*b_count

t=int(n-(0.1)//2)
for i in range(1,t):
    for j in range(n-2*i):
        s_1 = s[j]
        s_2 = s[j+i]
        s_3 = s[j+i+i]
        if s_1 !=s_3 and s_2!=s_3 and s_1!=s_2:
            ans-=1
print(ans)

Solution (Python):
n = int(input())
s = input()
l = [0,0,0]

for i in range(n):
    if(s[i] == 'R'):
        l[0] += 1
    elif(s[i] == 'G'):
        l[1] += 1
    else:
        l[2] += 1
p = l[0]*l[1]*l[2]
q = 0
for i in range(1,(n+1)//2):
    for j in range(n-i*2):
        if(s[j] != s[j+i] and s[j+i] != s[j+2*i] and s[j+i*2] != s[j]):
            q += 1
print(p-q)

Solution (Python):
import collections
n = int(input())
s = str(input())
rn = collections.Counter(s)['R']
gn = collections.Counter(s)['G']
bn = collections.Counter(s)['B']
ans = rn*gn*bn
if ans == 0:
     print(0)
elif n==3 and ans ==1:
     print(0)
elif n%2==0:
     for i in range(1,n//2):
          for j in range(n-2*i):
               if s[j] != s[j+i] and s[j+2*i] != s[j+i] and s[j] != s[j+2*i]:
                    ans-=1
     print(ans)
else:
     for i in range(1,n//2+1):
          for j in range(n-2*i):
               if s[j] != s[j+i] and s[j+2*i] != s[j+i] and s[j] != s[j+2*i]:
                    ans-=1
     print(ans)

Solution (Python):
import collections
import math
n = int(input())
s_list = list(input())
c = collections.Counter(s_list)
ans = c["R"] * c["G"] * c["B"]
for i in range(n):
    for j in range(i + 1, n):
        k = j + j - i
        if k < n:
            if s_list[i] != s_list[j] and s_list[i] != s_list[k] and s_list[j] != s_list[k]:
                ans -= 1
print(ans)

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

right=[[0]*(N+1) for _ in range(3)]
dic={'R':0,'G':1,'B':2}

for i in range(N,0,-1):
    for j in range(3):
        right[j][i-1]=right[j][i]

    c=S[i-1]
    right[dic[c]][i-1]+=1

cnt=0

for i in range(N-2):
    for j in range(i,N-1):
        a=S[i]
        b=S[j]
        if a==b:
            continue
        k=0
        while dic[a]==k or dic[b]==k:
            k+=1
        
        if 2*j-i<N and dic[S[2*j-i]]==k:
            cnt+=right[k][j+1]-1
        else:
            cnt+=right[k][j+1]
            

print(cnt)

Solution (Python):
N = int(input())
S = list(input())
k = [0] * N
for i in range(N):
    if S[i] == "R":
        k[i] = 2
    elif S[i] == "G":
        k[i] = 3
    else:
        k[i] = 5
a = k.count(2)
b = k.count(3)
c = N - a - b
ans = a*b*c
for i in range (1,(N + 1)//2):
    for j in range (N - 2 * i):
        if k[j] * k[j + i] * k[j + 2 * i] == 30:
            ans = ans - 1
print(ans)

Solution (Python):
import sys
from bisect import bisect_right as bi_r, bisect_left as bi_l

n, s = sys.stdin.read().split()
n = int(n)
s = s.replace('R', '0')
s = s.replace('G', '1')
s = s.replace('B', '2')
s = [int(x) for x in s]

def main():
    res = [[] for _ in range(3)]
    for i in range(n):
        res[s[i]].append(i)
    tot = 0
    for j in range(1, n - 1):
        cnt = 0
        m = s[j]
        if m == 0:
            lr = [(1, 2), (2, 1)]
        elif m == 1:
            lr = [(0, 2), (2, 0)]
        else:
            lr = [(0, 1), (1, 0)]
        for l, r in lr:
            cnt += bi_r(res[l], j) * (len(res[r]) - bi_l(res[r], j))
        i = j - 1
        k = j + 1
        while True:
            if i < 0 or k >= n:
                break
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt -= 1
            i -= 1
            k += 1
        tot += cnt
    print(tot)

if __name__ ==  '__main__':
    main()

Solution (Python):
import collections
import sys
n=int(input())
if n<=2:
  print(0)
  sys.exit()
s=input()
S=list(s)
t=collections.Counter(S)
R=t['R']
G=t['G']
B=t['B']
num=R*G*B
cnt=0
for i in range(1,(n//2)+1):
  for j in range(n):
    if j+(2*i)>n-1:
      continue
    if s[j]=='R':
      if s[j+i]=='G':
        if s[j+(2*i)]=='B':
          cnt+=1
      if s[j+i]=='B':
        if s[j+(2*i)]=='G':
          cnt+=1
    if s[j]=='G':
      if s[j+i]=='R':
        if s[j+(2*i)]=='B':
          cnt+=1
      if s[j+i]=='B':
        if s[j+(2*i)]=='R':
          cnt+=1
    if s[j]=='B':
      if s[j+i]=='G':
        if s[j+(2*i)]=='R':
          cnt+=1
      if s[j+i]=='R':
        if s[j+(2*i)]=='G':
          cnt+=1
print(num-cnt)

Solution (Python):
n = int(input())
s = list(input())
ans = 0
r = [0 for i in range(n)]
g = [0 for i in range(n)]
b = [0 for i in range(n)]
for i in range(n):
    if s[i] == "R":
        r[i] += 1
    elif s[i] == "G":
        g[i] += 1
    else:
        b[i] += 1
    if i > 0:
        r[i] += r[i-1]
        g[i] += g[i-1]
        b[i] += b[i-1]
ju = [0,0,0]
ju2 = ["R","G","B"]
for i in range(n-2):
    for j in range(3):
        if s[i] == ju2[j]:
            ju[j] += 1
    for j in range(i+1,n-1):
        
        for k in range(3):
            if s[j] == ju2[k]:
                ju[k] += 1
                key = k
        if ju[key] == 1:
            if ju[0] == 0:
                ans += r[n-1] - r[j]
                if j + (j-i) < n:
                    if r[j+(j-i)] > r[j+(j-i)-1]:
                        ans -= 1
            
            elif ju[1] == 0:
                ans += g[n-1] - g[j]
                if j + (j-i) < n:
                    if g[j+(j-i)] > g[j+(j-i)-1]:
                        ans -= 1
            else:
                ans += b[n-1] - b[j]
                if j + (j-i) < n:
                    if b[j+(j-i)] > b[j+(j-i)-1]:
                        ans -= 1
        ju[key] -= 1
    ju = [0,0,0]
print(ans)

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

d = {
    'R': [0],
    'G': [0],
    'B': [0]
}

for i in reversed(range(n)):
    for c in 'RGB':
        if s[i] == c:
            d[c].append(d[c][-1]+1)
        else:
            d[c].append(d[c][-1])
ans = 0

for c1, c2, c3 in itertools.permutations(['R', 'G', 'B'], 3):
    for i in range(n):
        if s[i] == c1:
            for j in range(i+1, n-1):
                if s[j] == c2:
                    ans += d[c3][-j-1]
                    l = 2 * j -i
                    if l < n and s[l] == c3:
                        ans -= 1
    
print(ans)

Solution (Python):
N = int(input().strip())

S = input()

count = [[0,0,0] for i in range(N)]
for i in range(N-1, 0, -1):
    for j in range(3):
        count[i-1][j] = count[i][j]
    if S[i] == "R":
        count[i-1][0] += 1
    elif S[i] == "G":
        count[i-1][1] += 1
    elif S[i] == "B":
        count[i-1][2] += 1
"""
for j in range(3):
    for i in range(N):
        print(count[i][j], end = " ")
    print()
"""
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        if S[i] == S[j]:
            continue
        if (S[i] == "R" and S[j] == "G") or (S[i] == "G" and S[j] == "R"):
            ans += count[j][2]
            diff = j-i
            if j+diff < N and S[j+diff] == "B":
                ans -= 1
        elif (S[i] == "B" and S[j] == "R") or (S[i] == "R" and S[j] == "B"):
            ans += count[j][1]
            diff = j-i
            if j+diff < N and S[j+diff] == "G":
                ans -= 1
        elif (S[i] == "G" and S[j] == "B") or (S[i] == "B" and S[j] == "G"):
            ans += count[j][0]
            diff = j-i
            if j+diff < N and S[j+diff] == "R":
                ans -= 1
print(ans)

Solution (Python):

import sys


def solve(N: int, S: str):
    R = set()
    G = set()
    B = set()
    for i in range(N):
        if S[i] == 'R':
            R.add(i)
        elif S[i] == 'G':
            G.add(i)
        else:
            B.add(i)
    
    d = 0
    for r in R:
        for g in G:
            x = min(r, g)
            y = max(r, g)
            if (2 * y - x) in B:
                d += 1
            if (2 * x - y) >= 0 and (2 * x - y) in B:
                d += 1
            if (y + x) / 2 == int((y + x) / 2) and (y + x) / 2 in B:
                d += 1
    print(len(R) * len(G) * len(B) - d)
    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):
from collections import Counter
n = int(input())
s = list(input())
l = [0]*3
for i in s:
	if i == "R":
		l[0]+=1
	elif i == "G":
		l[1]+=1
	else:
		l[2]+=1
su = l[0]*l[1]*l[2]
for i in range(1,n+1):
	for j in range(i+2,n+1,2):
		kk = (i+j)//2
		if s[i-1]!=s[j-1] and s[i-1]!=s[kk-1] and s[j-1]!=s[kk-1]:
			su-=1
print(su)

Solution (Python):
def main():
    input()
    r, g, b = set(), set(), set()
    for i, s in enumerate(input()):
        if s == "R":
            r.add(i + 1)
        if s == "G":
            g.add(i + 1)
        if s == "B":
            b.add(i + 1)
    c = len(r) * len(g) * len(b)
    for i in r:
        for j in g:
            k = abs(i - j)
            if i + k in b:
                c -= 1
            if i - k in b:
                c -= 1
            if j + k in b:
                c -= 1
            if j - k in b:
                c -= 1
            if (i + j) / 2 in b:
                c -= 1
    print(c)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
r = 0
g = 0
b = 0
for i in range(n):
    if(s[i]=='R'):r+=1
    if(s[i]=='G'):g+=1
    if(s[i]=='B'):b+=1
rem = 0
for i in range(n-2):
    width = 1
    while i+width*2<n:
        if(s[i]!=s[i+width] and s[i+width]!=s[i+width*2] and s[i+width*2]!=s[i]):
            rem += 1
        width += 1
print(r*g*b-rem)

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

M = [[0] * 3 for _ in range(N+1)]

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

ans = 0

T = "RGB"


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

        k = j + (j-i)
        if S[j] == "R":
            if m[0] == 0:
                m[0] += 1
                idx = m.index(0)
                ans += M[j+1][idx]
                if k < N and S[k] == T[idx]:
                    ans -= 1
            else:
                continue
        elif S[j] == "G":
            if m[1] == 0:
                m[1] += 1
                idx = m.index(0)
                ans += M[j+1][idx]
                if k < N and S[k] == T[idx]:
                    ans -= 1
            else:
                continue
        else:
            if m[2] == 0:
                m[2] += 1
                idx = m.index(0)
                ans += M[j+1][idx]
                if k < N and S[k] == T[idx]:
                    ans -= 1
            else:
                continue

print(ans)

Solution (Python):
if __name__ == "__main__":
    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] == 'G':
            G.add(i)
        elif S[i] == 'B':
            B.add(i)

    except_cnt = 0
    for w in range(1, N//2 + 1):
        for i in range(N):
            j = i + w
            k = j + w
            if i in R and j in G and k in B:
                except_cnt += 1
            if i in R and j in B and k in G:
                except_cnt += 1
            if i in B and j in R and k in G:
                except_cnt += 1
            if i in B and j in G and k in R:
                except_cnt += 1
            if i in G and j in B and k in R:
                except_cnt += 1
            if i in G and j in R and k in B:
                except_cnt += 1

    print(len(R) * len(G) * len(B) - except_cnt)