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


def solve(N: int, S: str):
    C = [[0 for i in range(3)] for j in range(N+1)]
    for i in range(N-1,-1,-1):
        if S[i] == "R":
            C[i][0] = C[i+1][0]+1
            C[i][1] = C[i+1][1]
            C[i][2] = C[i + 1][2]
        elif S[i] == "G":
            C[i][0] = C[i + 1][0]
            C[i][1] = C[i + 1][1]+1
            C[i][2] = C[i + 1][2]
        else:
            C[i][0] = C[i + 1][0]
            C[i][1] = C[i + 1][1]
            C[i][2] = C[i + 1][2]+1
    sum = 0
    
    for i in range(N):
        for j in range(i+1,N-1):
            if S[i] == S[j]:
                continue
            k = j - i + j
            
            if S[i] != "R" and S[j] != "R":
                sum += C[j+1][0]
                if k < N and S[k] == "R":
                    sum -=1
                
            if S[i] != "G" and S[j] != "G":
                sum += C[j + 1][1]
                if k < N and S[k] == "G":
                    sum -= 1
                
            if S[i] != "B" and S[j] != "B":
                sum += C[j + 1][2]
                if k < N and S[k] == "B":
                    sum -= 1
                
    print(sum)
    return



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

if __name__ == '__main__':
    main()

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

dict={'R':1,'G':2,'B':3}
num={1:'R',2:'G',3:'B'}

snum=[[0,0,0] for i in range(n)]
for i in range(n):
    if i == 0:
        snum[0][dict[s[i]]-1] = 1
        continue
    for j in range(3):
        if dict[s[i]]-1 == j:
            snum[i][j] = snum[i-1][j] + 1
        else:
            snum[i][j] = snum[i-1][j]

ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        temp=s[i]
        temp1=s[j]
        if temp == temp1:continue

        temp_num=6-dict[temp]-dict[temp1]
        temp2=num[temp_num]
        count=snum[-1][temp_num-1]-snum[j][temp_num-1]
        
        if 2*j-i<n and s[2*j-i] == temp2:
            count-=1
        ans+=count
print(ans)

Solution (Python):
n=int(input())
srgb=input()
red=srgb.count("R")
green=srgb.count("G")
blue=n-red-green
ans=red*green*blue
counter=0
for i in range(n):
    for j in range(i+1,n):
        k=j-i+j
        if k<n:
            if srgb[i]!=srgb[j] and srgb[j]!=srgb[k] and srgb[k]!=srgb[i]:
                counter+=1
ans-=counter
print(ans)

Solution (Python):
n,s=int(input()),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 s[i]!=s[j]:
            if {s[i],s[j]}=={"R","G"}:
                ans+=b[-1]-b[j]
                if 2*j-i<n and s[2*j-i]=="B":
                    ans-=1
            if {s[i],s[j]}=={"R","B"}:
                ans+=g[-1]-g[j]
                if 2*j-i<n and s[2*j-i]=="G":
                    ans-=1
            if {s[i],s[j]}=={"B","G"}:
                ans+=r[-1]-r[j]
                if 2*j-i<n and s[2*j-i]=="R":
                    ans-=1           
print(ans)

Solution (Python):
n=int(input())
s=input()
cnt=[[0,0,0] for _ in range(n)]
for i in range(n):
    if s[i]=='R':
        cnt[i][0]+=1
    elif s[i]=='G':
        cnt[i][1]+=1
    elif s[i]=='B':
        cnt[i][2]+=1
acum=[[0,0,0]]
for i in range(n):
    tmp=[0,0,0]
    for j in range(3):
        tmp[j]+=acum[-1][j]
        tmp[j]+=cnt[i][j]
    acum.append(tmp)
ans=0
for i in range(n):
    for j in range(i+1,n):
        if s[i]==s[j]:
            continue
        if 'R' not in (s[i],s[j]):
            ans+=acum[-1][0]-acum[j][0]
            if 2*j-i<n and s[2*j-i]=='R':
                ans-=1
        elif 'G' not in (s[i],s[j]):
            ans+=acum[-1][1]-acum[j][1]
            if 2*j-i<n and s[2*j-i]=='G':
                ans-=1
        elif 'B' not in (s[i],s[j]):
            ans+=acum[-1][2]-acum[j][2]
            if 2*j-i<n and s[2*j-i]=='B':
                ans-=1
print(ans)

Solution (Python):
N = int(input())
S = 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)

Bs = set(B)
ans = 0
for r in R:
    for g in G:
        l, g = min(r, g), max(r, g)
        ans += len(B)
        ans -= (2*g - l) in Bs
        ans -= (2*l - g) in Bs
        ans -= (g + l) % 2 == 0 and (g + l)//2 in Bs
print(ans)

Solution (Python):
n = int(input())
s = list(input())
cnt = {
    "R": 0,
    "G": 0,
    "B": 0
}

for i, c in enumerate(s):
    cnt[c] += 1

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

print(ans-d)

Solution (Python):

import sys
import numpy as np

sys.setrecursionlimit(10 ** 9)

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

rgb = [0,0,0]
for i in range(N):
    if S[i] == 'R':
        rgb[0] += 1
    elif S[i] == 'G':
        rgb[1] += 1
    else:
        rgb[2] += 1

ans = rgb[0] * rgb[1] * rgb [2]


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

print(ans)

Solution (Python):

import sys


def solve(N: int, S: str):
    R = [0]*N
    G = [0]*N
    B = [0]*N
    for i in range(N-1,-1,-1):
        if S[i] == "R":
            if i == N-1:
                R[i] = 1
            else:
                R[i] = R[i+1] + 1 
                G[i] = G[i+1]
                B[i] = B[i+1]
        elif S[i] == "G":
            if i == N-1:
                G[i] = 1
            else:
                R[i] = R[i+1] 
                G[i] = G[i+1] + 1
                B[i] = B[i+1]
        elif S[i] == "B":
            if i == N-1:
                B[i] = 1
            else:
                R[i] = R[i+1] 
                G[i] = G[i+1]
                B[i] = B[i+1]+1

    answer = 0
    for i in range(N-2):
        for j in range(i+1,N-1):
            if S[i] == "R":
                if S[j] == "G":
                    answer += B[j+1]
                    if 2*j-i < N and S[2*j-i] == "B":
                        answer -= 1
                elif S[j] == "B":
                    answer += G[j+1]
                    if 2*j-i < N and S[2*j-i] == "G":
                        answer -= 1
            elif S[i] == "G":
                if S[j] == "R":
                    answer += B[j+1]
                    if 2*j-i < N and S[2*j-i] == "B":
                        answer -= 1
                elif S[j] == "B":
                    answer += R[j+1]
                    if 2*j-i < N and S[2*j-i] == "R":
                        answer -= 1
            elif S[i] == "B":
                if S[j] == "G":
                    answer += R[j+1]
                    if 2*j-i < N and S[2*j-i] == "R":
                        answer -= 1
                elif S[j] == "R":
                    answer += G[j+1]
                    if 2*j-i < N and S[2*j-i] == "G":
                        answer -= 1

    print(answer)
    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 deque
from bisect import bisect_left

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

d = {s:[] for s in "RGB"}
for i, s in enumerate(S):
  d[s].append(i)
if d["R"] and d["G"] and d["B"]:
  ans = len(d["R"])*len(d["G"])*len(d["B"])
  for r in d["R"]:
    for g in d["G"]:
      M = max(r, g)
      m = r+g-M
      if M+M-m < N and S[M+(M-m)] == "B":
        ans -= 1
      if m-(M-m) >= 0 and S[m-(M-m)] == "B":
        ans -= 1
      if (M+m)%2 == 0 and S[(M+m)//2] == "B":
        ans -= 1
  print(ans)
else:
  print(0)

Solution (Python):
N = int(input())
S = input()
R = [0]*N
G = [0]*N
B = [0]*N
for k in range(N):
    if S[-k-1] == "R":
        R[-k-1] = 1
    elif S[-k-1] == "G":
        G[-k-1] = 1
    else:
        B[-k-1] = 1

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

ans = 0
for k in range(N):
    if S[k] == "R":
        ans += G[k+1]*B[k+1]
    elif S[k] == "G":
        ans += R[k+1]*B[k+1]
    else:
        ans += R[k+1]*G[k+1]

for k in range(1+N//2):
    l = 0
    while l+2*k < N:
        if S[l] != S[l+k] and S[l+k] != S[l+2*k] and S[l+2*k] != S[l]:
            ans -= 1
        l += 1
print(ans)

Solution (Python):
K = int(input())
S = input()
r = 0
g = 0
b = 0
for c in S:
  if c=="R":
    r+=1
  if c=="G":
    g+=1
  if c=="B":
    b+=1
    
ans = r*g*b

for i in range(1,2000):
  tail = i*2
  if tail>(K-1):
    break
  head = 0
  mid = i
  while tail<=(K-1):
    if S[head]!=S[mid] and S[tail]!=S[mid] and S[head]!=S[tail]:
      ans = ans-1
    head+=1
    mid+=1
    tail+=1
    
print(ans)

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

locs = {}
RGB = {"R", "G", "B"}

for let in RGB:
    lc = []
    temp = 0
    for l in reversed(s):
        if l == let:
            temp += 1
        lc.append(temp)
    locs[let] = list(reversed(lc))

total = 0
for i in range(0, n - 2):
    il = s[i]
    for j in range(i + 1, n - 1):
        jl = s[j]
        if il != jl:
            left = (RGB - {il, jl}).pop()
            local = locs[left][j + 1]
            mk = j + (j - i)
            if mk < n and s[mk] == left:
                local -= 1
            total += local

print(total)

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

bad_count = 0

for middle in range(1, n - 1):
	left = middle - 1
	right = middle + 1
	while left >= 0 and right < n:
		if s[middle] != s[left] and s[middle] != s[right] and s[right] != s[left]:
			bad_count += 1



		left -= 1
		right += 1

left_r = 0
left_g = 0
left_b = 0
right_r = 0
right_g = 0
right_b = 0

for c in s:
	if c == 'R':
		right_r += 1
	elif c == 'G':
		right_g += 1
	else:
		right_b += 1

good_count = 0

for c in s:
	if (c == 'R'):
		good_count += left_b * right_g
		good_count += right_b * left_g
		left_r += 1
		right_r -= 1
	elif c == 'G':
		good_count += left_r * right_b
		good_count += right_r * left_b
		left_g += 1
		right_g -= 1
	else:
		good_count += left_r * right_g
		good_count += right_r * left_g
		left_b += 1
		right_b -= 1

print(good_count - bad_count)

Solution (Python):
import collections

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

c = collections.Counter(S)
if 'R' in c.keys() and 'G' in c.keys() and 'B' in c.keys():
    ans = c['R'] * c['G'] * c['B']

    for i in range(N-2):
        for x in range(((N-i)-1)//2):
            if S[i] != S[i+x+1] and S[i] != S[i+2*(x+1)] and S[i+x+1] != S[i+2*(x+1)]:
                ans -= 1
else:
    ans = 0
print(ans)

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



sc = Counter(s)
ans_c1 = sc['R']*sc['G']*sc['B']



r_idx = {i for i in range(n) if s[i]=='R'}
g_idx = {i for i in range(n) if s[i]=='G'}
b_idx = {i for i in range(n) if s[i]=='B'}

count = 0
for ri in r_idx:
    for gi in g_idx:
        if (ri+gi)%2==0 and (ri+gi)//2 in b_idx:
            count+=1

for ri in r_idx:
    for bi in b_idx:
        if (ri+bi)%2==0 and (ri+bi)//2 in g_idx:
            count+=1

for bi in b_idx:
    for gi in g_idx:
        if (bi+gi)%2==0 and (bi+gi)//2 in r_idx:
            count+=1

print(ans_c1-count)

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

rcs = [0] * N
gcs = [0] * N
bcs  = [0] * N

for i in range(N):
    if S[i] == 'R':
        rcs[i] = 1
    elif S[i] == 'G':
        gcs[i] = 1
    elif S[i] == 'B':
        bcs[i] = 1

for i in range(1, N):
    rcs[i] += rcs[i - 1]
    gcs[i] += gcs[i - 1]
    bcs[i] += bcs[i - 1]

result = 0
for i in range(N):
    if S[i] == 'R':
        for j in range(i + 1, N):
            if S[j] == 'R':
                continue
            elif S[j] == 'G':
                result += bcs[N - 1] - bcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'B':
                    result -= 1
            elif S[j] == 'B':
                result += gcs[N - 1] - gcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'G':
                    result -= 1
    elif S[i] == 'G':
        for j in range(i + 1, N):
            if S[j] == 'G':
                continue
            elif S[j] == 'R':
                result += bcs[N - 1] - bcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'B':
                    result -= 1
            elif S[j] == 'B':
                result += rcs[N - 1] - rcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'R':
                    result -= 1
    elif S[i] == 'B':
        for j in range(i + 1, N):
            if S[j] == 'B':
                continue
            elif S[j] == 'R':
                result += gcs[N - 1] - gcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'G':
                    result -= 1
            elif S[j] == 'G':
                result += rcs[N - 1] - rcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'R':
                    result -= 1
print(result)

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





nums = [[0 for i in range(n)] for j in range(3)]

ans = 0

befs = [0 for i in range(3)]
for i in range(n):
    if s[i] == "R":
        befs[0] += 1
    elif s[i] == "G":
        befs[1] += 1
    else:
        befs[2] += 1
    for j in range(3):
        nums[j][i] = befs[j]

vac = [[-1,2,1],
       [2,-1,0],
       [1,0,-1]]
tr = {"R":0,"G":1,"B":2}

for i in range(n):
    for j in range(i+1,n-1):
        if s[i] == s[j]:
            continue
        else:
            v1 = tr[s[i]]
            v2 = tr[s[j]]
            nex = vac[v1][v2]
            
            tp = nums[nex][-1]-nums[nex][j+1]
            if tr[s[j+1]] == nex:
                tp += 1
            if j+j-i <= n-1 and tr[s[j+j-i]] == nex:
                tp -= 1
            ans += tp

print(ans)

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



dp = [[[[0 for _ in range(2)] for _ in range(2)] for _ in range(2)] for _ in range(n+10)]

dp[0][0][0][0] = 1
for i in range(n):
    ch = s[i]
    
    for p in range(2):
        for q in range(2):
            for r in range(2):
                dp[i+1][p][q][r] = dp[i][p][q][r]

    if ch == 'R':
        dp[i+1][1][0][0] += dp[i][0][0][0]
        dp[i+1][1][1][0] += dp[i][0][1][0]
        dp[i+1][1][0][1] += dp[i][0][0][1]
        dp[i+1][1][1][1] += dp[i][0][1][1]
    elif ch == 'G':
        dp[i+1][0][1][0] += dp[i][0][0][0]
        dp[i+1][0][1][1] += dp[i][0][0][1]
        dp[i+1][1][1][0] += dp[i][1][0][0]
        dp[i+1][1][1][1] += dp[i][1][0][1]
    else:
        dp[i+1][0][0][1] += dp[i][0][0][0]
        dp[i+1][1][0][1] += dp[i][1][0][0]
        dp[i+1][0][1][1] += dp[i][0][1][0]
        dp[i+1][1][1][1] += dp[i][1][1][0]

ans = dp[n][1][1][1]





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


print(ans)

Solution (Python):
from collections import Counter
n = int(input())
s = input()
flag = True
if len(set(s))<=2:
    print(0)
    flag = False
if flag:
    r = [i+1 for i in range(n) if s[i]=='R']
    b = [i+1 for i in range(n) if s[i]=='B']
    g = [i+1 for i in range(n) if s[i]=='G']
    minus = 0
    def cnt_cmb(a, b, c):
        cnt = 0
        set_c = set(c)
        for i in a:
            for j in b:
                if (i+j)/2 in set_c:
                    cnt += 1
        return cnt
    minus +=cnt_cmb(r, b, g)
    minus += cnt_cmb(b, g, r)
    minus += cnt_cmb(g, r, b)
    a = Counter(s)
    rslt = 1
    for value in a.values():
        rslt *= value
    print(rslt-minus)

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

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

del_res=0
for i in range(1,n-1):
    for q in range(1,min(i,n-i-1)+1):
        
        if s[i]!=s[i-q] and s[i]!=s[i+q] and s[i-q]!=s[i+q]:
            del_res+=1

print(all_res-del_res)

Solution (Python):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
s = list(map(ord, ns()))
d = dict()
for x in map(ord, list('RGB')):
    d[x] = s.count(x)
tot = sum(map(ord, list('RGB')))
ans = 0
for i in range(n-1):
    for j in range(i):
        if s[i] != s[j]:
            t = tot - s[i] - s[j]
            ans += d[t] - ((2*i - j) < n and t == s[2*i - j])
    d[s[i]] -= 1
print(ans)

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
S = input()
C = collections.Counter(S)
R = [0] * (n+1)
G = [0] * (n+1)
B = [0] * (n+1)
for i,si in enumerate(S):
  R[i+1] += R[i]
  G[i+1] += G[i]
  B[i+1] += B[i]
  if si == "R":R[i+1] += 1
  elif si == "G":G[i+1] += 1
  else:B[i+1] += 1
for i in range(1,n-1):
  si = S[i]
  if si == "R":
    ans += G[i] * (B[n]-B[i+1]) + B[i] * (G[n]-G[i+1])
  elif si == "G":
    ans += R[i] * (B[n]-B[i+1]) + B[i] * (R[n]-R[i+1])
  else:
    ans += G[i] * (R[n]-R[i+1]) + R[i] * (G[n]-G[i+1])




    
for span in range(1,n):
  for i in range(n-2*span):
    if S[i] != S[i+span] and S[i+span] != S[i+span+span] and S[i+span+span] != S[i]:
      ans-=1
print(ans)

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

ans = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    if s[i] == s[j]:
      continue
    key = ["R", "G", "B"]
    key.remove(s[i])
    key.remove(s[j])
    k = key[0]
    if 2*j-i >= n:
      if k == "R":
        ans += r[j+1]
      if k == "G":
        ans += g[j+1]
      if k == "B":
        ans += b[j+1]
      continue
    if s[i] != s[2*j-i] and s[j] != s[2*j-i]:
      if k == "R":
        ans += r[j+1]-1
      if k == "G":
        ans += g[j+1]-1
      if k == "B":
        ans += b[j+1]-1
    else:
      if k == "R":
        ans += r[j+1]
      if k == "G":
        ans += g[j+1]
      if k == "B":
        ans += b[j+1]
    

print(ans)

Solution (Python):
def main():
    n=int(input())
    s=input()
    l1=[0]*(n)
    l2=[0]*(n)
    l3=[0]*(n)
    for i in range(1,n):
        if s[i]=="R":
            l1[i-1]+=1
        elif s[i]=="G":
            l2[i-1]+=1
        else:
            l3[i-1]+=1
    for i in range(n-1,0,-1):
        l1[i-1]+=l1[i]
        l2[i-1]+=l2[i]
        l3[i-1]+=l3[i]
    ans=0
    for i in range(n):
        for j in range(i+1,n):
            if set([s[i],s[j]])=={"G","B"}:
                ans+=l1[j]
                if 2*j-i<n and s[2*j-i]=="R":
                    ans-=1
            elif set([s[i],s[j]])=={"R","B"}:
                ans+=l2[j]
                if 2*j-i<n and s[2*j-i]=="G":
                    ans-=1
            elif set([s[i],s[j]])=={"R","G"}:
                ans+=l3[j]
                if 2*j-i<n and s[2*j-i]=="B":
                    ans-=1
    print(ans)
if __name__ == '__main__':
    main()

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

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

d = 1
while True:
    if 2 * d >= N:
        break
    i = 0
    while True:
        j = i + d
        k = j + d
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
        i += 1
    d += 1

print(ans)

Solution (Python):
n = int(input())
s = str(input())
R = s.count("R")
B = s.count("B")
G = s.count("G")
ans = R*G*B
cnt = 0
for i in range(2000):
    for j in range(0, n-2*i):
        if s[j] != s[j+i] and s[j+i] != s[j+2*i] and s[j] != s[j+2*i]:
            cnt += 1
print(ans - cnt)

Solution (Python):
n=int(input())
S=list(input())
R=[0 for _ in range(n)]
G=[0 for _ in range(n)]
B=[0 for _ in range(n)]
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
    R[i]=r; G[i]=g; B[i]=b
cnt=0
for i in range(n-2):
    for j in range(i,n-1):
        if S[i]==S[j]:
            continue
        if (S[i]=="R" or S[i]=="G") and (S[j]=="R" or S[j]=="G"):
            cnt+=B[-1]-B[j]
            try:
                if S[2*j-i]=="B":
                    cnt-=1
            except:
                pass
        if (S[i]=="G" or S[i]=="B") and (S[j]=="G" or S[j]=="B"):
            cnt+=R[-1]-R[j]
            try:
                if S[2*j-i]=="R":
                    cnt-=1
            except:
                pass
        if (S[i]=="B" or S[i]=="R") and (S[j]=="B" or S[j]=="R"):
            cnt+=G[-1]-G[j]
            try:
                if S[2*j-i]=="G":
                    cnt-=1
            except:
                pass
print(cnt)

Solution (Python):
from collections import Counter

n = int(input())
s = input()
c = Counter(s)
a = c['R'] * c['G'] * c['B']

def search(l, r, a, t):
    while (r-l)>=0:
        mid = (l+r)//2
        if a[mid] == t:
            return True
        if a[mid] > t:
            r = mid-1
        if a[mid] < t:
            l = mid+1

    return False

ri = []
gi = []
bi = []

for i in range(n):
    if s[i] == "R":
        ri.append(i)
    elif s[i] == "G":
        gi.append(i)
    elif s[i] == "B":
        bi.append(i)
        
for i in range(len(gi)):
    for j in range(len(bi)):
        if search(0, len(ri)-1, ri, bi[j]-abs(gi[i]-bi[j])) or search(0, len(ri)-1, ri, gi[i]-abs(gi[i]-bi[j])):
            a -= 1
            
for i in range(len(bi)):
    for j in range(len(ri)):
        if search(0, len(gi)-1, gi, ri[j]-abs(bi[i]-ri[j])) or search(0, len(gi)-1, gi, bi[i]-abs(bi[i]-ri[j])):
            a -= 1
            
for i in range(len(ri)):
    for j in range(len(gi)):
        if search(0, len(bi)-1, bi, gi[j]-abs(ri[i]-gi[j])) or search(0, len(bi)-1, bi, ri[i]-abs(ri[i]-gi[j])):
            a -= 1
            
print(a)

Solution (Python):


import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(0)
	n = int(sys.stdin.readline())
	s = input()
	sumc = []
	sumr = 0
	sumg = 0
	sumb = 0
	for i in range(n):
		if s[i] == "R":
			sumr += 1
		if s[i] == "G":
			sumg += 1
		if s[i] == "B":
			sumb += 1
		sumc.append({
			"R" : sumr,
			"G": sumg,
			"B": sumb
			})
	a = {"R": 0, "G": 1, "B": 2}
	case = 0
	log.print(sumc)
	for i in range(n):
		for j in range(i+1, n-1):
			l = j - i
			if s[i] == s[j]:
				continue
			rgb = list("RGB")
			rgb.pop(rgb.index(s[i]))
			rgb.pop(rgb.index(s[j]))
			case += sumc[n-1][rgb[0]] - sumc[j][rgb[0]]
			if j + l < n and s[j+l] == rgb[0]:
				case -= 1
			log.print(i, j, rgb, case)
	print(case)




	


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(1, (N - 1) // 2 + 1):
    for j in range(N - i * 2):
        if 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):
N = int(input())

S = input()
listR = []
listB = []
listG = []
numR = 0
numG = 0
numB = 0

for idx,i in enumerate(S):
    if i == 'R':
        numR += 1
        listR.append((idx, numR, numG, numB))
    elif i == 'G':
        numG += 1
        listG.append((idx, numR, numG, numB))
    else:
        numB += 1
        listB.append((idx, numR, numG, numB))

result = 0
patterns = [
    (listR, listG, (3, "B", numB)),
    (listR, listB, (2, "G", numG)),
    (listG, listR, (3, "B", numB)),
    (listG, listB, (1, "R", numR)),
    (listB, listG, (1, "R", numR)),
    (listB, listR, (2, "G", numG)),
]

for list1, list2, str3 in patterns:
    for datR in list1:
        for dat2 in list2:
            idx = dat2[0]
            if idx > datR[0]:
                result += str3[2] - dat2[str3[0]]
                k = idx + (idx - datR[0])
                if k < N and S[k] == str3[1]:
                    result -= 1
print(result)

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

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

a = 0
for i in range(n):
    d = 1
    while i+2*d <= n-1:
        if s[i] != s[i+d] and s[i+d] != s[i+2*d] and s[i] != s[i+2*d]:
            a += 1
        d += 1

print (count['R']*count['G']*count['B'] - a)

Solution (Python):
N=int(input())
S=list(input())
rc,bc,gc=[0]*N,[0]*N,[0]*N
if S[0]=="R":
  rc[0]+=1
elif S[0]=="B":
  bc[0]+=1
if S[0]=="G":
  gc[0]+=1
for i in range(1,N):
  if S[i]=="R":
    rc[i]=rc[i-1]+1
    bc[i]=bc[i-1]
    gc[i]=gc[i-1]
  elif S[i]=="B":
    bc[i]=bc[i-1]+1
    gc[i]=gc[i-1]
    rc[i]=rc[i-1]
  elif S[i]=="G":
    gc[i]=gc[i-1]+1    
    rc[i]=rc[i-1]
    bc[i]=bc[i-1]

ans=0
for i in range(N-2):
  for j in range(i+1,N-1):
    if S[i]==S[j]:
      pass
    else:
      if [S[i],S[j]]==["R","B"] or [S[i],S[j]]==["B","R"]:
        ans+=gc[-1]-gc[j]
        if 2*j-i<N:
          if S[2*j-i]=="G":
            ans-=1
      elif [S[i],S[j]]==["B","G"] or [S[i],S[j]]==["G","B"]:
        ans+=rc[-1]-rc[j]
        if 2*j-i<N:
          if S[2*j-i]=="R":
            ans-=1
      elif [S[i],S[j]]==["G","R"] or [S[i],S[j]]==["R","G"]:
        ans+=bc[-1]-bc[j]
        if 2*j-i<N:
          if S[2*j-i]=="B":
            ans-=1
            
print(ans)

Solution (Python):
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

r_sum = [0] * N
g_sum = [0] * N
b_sum = [0] * N

if S[0] == "R":
    r_sum[0] = 1
elif S[0] == "G":
    g_sum[0] = 1
else:
    b_sum[0] = 1

c_map = {"R": 1, "G": 2, "B": 3}

for i, c in enumerate(S):
    if i == 0:
        continue
    r_sum[i] = r_sum[i-1]
    g_sum[i] = g_sum[i-1]
    b_sum[i] = b_sum[i-1]
    if c == "R":
        r_sum[i] += 1
    elif c == "G":
        g_sum[i] += 1
    else:
        b_sum[i] += 1

ans = 0
for i in range(N):
    for j in range(i+1, N):
        d = j - i
        if S[i] != S[j]:
            rest = 6 - c_map[S[i]] - c_map[S[j]]
            if rest == 1:
                ans += r_sum[N - 1] - r_sum[j]
                if j + d <= N - 1 and c_map[S[j + d]] == 1:
                    ans -= 1
            elif rest == 2:
                ans += g_sum[N - 1] - g_sum[j]
                if j + d <= N - 1 and c_map[S[j + d]] == 2:
                    ans -= 1
            else:
                ans += b_sum[N - 1] - b_sum[j]
                if j + d <= N - 1 and c_map[S[j + d]] == 3:
                    ans -= 1

print(ans)

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

r=g=b=0
c=0

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

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

print(r*g*b-c)

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

colormap = {'R':0,'G':1,'B':2}
S = [colormap[c] for c in input()]

bads = 0
for i in range(N):
    for s in range(1,(N-i+1)//2):
        bads += int(S[i]^S[i+s]^S[i+2*s] == 3)

totals = [0]*3
for c in S:
    totals[c] += 1

print(totals[0]*totals[1]*totals[2] - bads)

Solution (Python):
N = int(input())
S = list(input())
s_r = []
s_g = []
s_b = []

for i in range(len(S)):
    if S[i] == 'R':
        s_r.append(i)
    elif S[i] == 'G':
        s_g.append(i)
    elif S[i] == 'B':
        s_b.append(i)

N = len(s_r)*len(s_g)*len(s_b)

s_b = set(s_b)
for i in range(len(s_r)):
    for j in range(len(s_g)):
        M = max(s_r[i],s_g[j])
        m = min(s_r[i],s_g[j])
        if M+(M-m) in s_b:
            N -= 1

        if (M-m) %2 == 0:
            if m + (M-m)//2 in s_b:
                N -= 1

        if m-(M-m) in s_b:
            N -= 1

print(N)

Solution (Python):
import bisect
n = int(input())
S = input()
r = []
g = []
b = []
for i, s in enumerate(S):
    if s == "R":
        r.append(i)
    elif s == "G":
        g.append(i)
    else:
        b.append(i)


ans = len(r) * len(g) * len(b)
i = 0
j = 1
while i + 2 * j < n:
    while i + 2 * j < n:
        if S[i] != S[i + j] != S[i + 2 * j] != S[i]:
            ans -= 1
        i += 1
    j += 1
    i = 0

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

n = n_in()
s = s_in()


from collections import Counter

c=Counter(s)

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


for i in range(n):
    d = 1
    while i+d+d < n:
        if s[i] != s[i+d] and s[i+d] != s[i+d+d] and s[i] != s[i+d+d]:
            res -= 1

        d += 1
    

print(res)

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

r=0
ListR = []
g=0
ListG = []
b=0
ListB = []
for i in range(N):
    if S[i] == 'R':
        r = r+1
        ListR.append(i)
    elif S[i] == 'G':
        g = g+1
        ListG.append(i)
    else:
        b = b+1
        ListB.append(i)
        
n = (N+1)//2
  
remove = 0
for i in range(1,n+1):
    for j in range(N-2*i):
        if S[j] != S[j+i] and S[j+i] != S[j+2*i] and S[j] != S[j+2*i]:
            remove = remove+1
            
print(r*g*b-remove)