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 collections

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

lenR=S.count("R")
lenG=S.count("G")
lenB=S.count("B")

count=0
for i in range(N-2):
    for j in range(i,N-1):
        k=2*j-i
        if k>=N:
            continue
        else:
            if (S[i] != S[j]) and (S[i]!=S[k]) and (S[j]!=S[k]):
                count+=1
    
print(lenR*lenG*lenB-count)

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

r_num = S.count('R')
g_num = S.count('G')
b_num = S.count('B')

num = r_num * g_num * b_num

for j in range(N):
    for i in range(j):
        k = j + (j - i)
        if k < N:
            if S[i] == S[j]:
                continue
            if S[i] == S[k]:
                continue
            if S[j] == S[k]:
                continue
            num -= 1

print(num)

Solution (Python):

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

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

all_case = r*g*b

excluded_case = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]: continue
        k = j*2-i
        if k>= N or S[k] == S[i] or S[k] == S[j]: continue
        excluded_case += 1

print(all_case-excluded_case)

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

dic = {k: 0 for k in "RGB"}

for i in range(n):
  dic[s[i]] += 1
  
res = dic['R'] * dic['G'] * dic['B']

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

print(res)

Solution (Python):
n,s=open(0)
r,g,b=map(s.count, "RGB");e=enumerate
print(r*g*b-sum(f!=g!=s[i+j]!=f for i,f in e(s)for j,g in e(s[i:-1:2])))

Solution (Python):
n, s = open(0)
r, g, b = map(s.count, "RGB")
e = enumerate
print(r*g*b - sum(f != g != s[i+j] != f for i, f in e(s) for j, g in e(s[i:-1:2])))

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-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[j] != s[k] and s[k] != s[i]:
      counter += 1
      
print(r*g*b - counter)

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

R = 0
G = 0
B = 0

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

Solution (Python):
N=int(input())
S=input()
rgb={"R":0,"G":0,"B":0}
for s in S:
    rgb[s]+=1
sum=rgb["R"]*rgb["G"]*rgb["B"]

for i in range(0,N-2):
    for interval in range(1,(N-i+1)//2):
        if S[i]!=S[i+interval] and S[i+interval]!=S[i+2*interval] and S[i]!=S[i+2*interval]:
            sum-=1
print(sum)

Solution (Python):
N = int(input())
S = str(input())
P = 0
R = S.count('R')
G = S.count('G')
B = S.count('B')
P = R*G*B
for i in range(1,(N//2)+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]:
                P -= 1
print(P)

Solution (Python):
def sort(a,b,c):
    l=[a,b,c]
    l.sort()
    return l

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

Solution (Python):
def sort(a,b,c):
    l=[a,b,c]
    l.sort()
    return l

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

Solution (Python):






n = int(input())
S = input()

countR = S.count('R')
countG = S.count('G')
countB = S.count('B')


total = countR * countG * countB

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

total -= count

print(total)

Solution (Python):

from collections import defaultdict


def main():
    _, S = open(0).read().split()
    indices, doubled_indices = defaultdict(list), defaultdict(set)
    for i, c in enumerate(S):
        indices[c].append(i)
        doubled_indices[c].add(2 * i)
    cnt = S.count("R") * S.count("G") * S.count("B")
    for x, y, z in ["RGB", "GBR", "BRG"]:
        cnt -= sum(i + j in doubled_indices[z] for i in indices[x] for j in indices[y])
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict


def main():
    _, S = open(0).read().split()
    indices, doubled_indices = defaultdict(list), defaultdict(set)
    for i, c in enumerate(S):
        indices[c].append(i)
        doubled_indices[c].add(2 * i)
    cnt = len(indices["R"]) * len(indices["G"]) * len(indices["B"])
    for x, y, z in ["RGB", "GBR", "BRG"]:
        cnt -= sum(i + j in doubled_indices[z] for i in indices[x] for j in indices[y])
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict


def main():
    _, S = open(0).read().split()
    indices, doubled_indices = defaultdict(list), defaultdict(set)
    for i, c in enumerate(S):
        indices[c].append(i)
        doubled_indices[c].add(2 * i)
    cnt = len(indices["R"]) * len(indices["G"]) * len(indices["B"])
    for x, y, z in ["RGB", "GBR", "BRG"]:
        cnt -= sum(i + j in doubled_indices[z] for i in indices[x] for j in indices[y])
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict


def main():
    _ = input()
    S = input().rstrip()
    indices, doubled_indices = defaultdict(list), defaultdict(set)
    for i, c in enumerate(S):
        indices[c].append(i)
        doubled_indices[c].add(2 * i)
    cnt = len(indices["R"]) * len(indices["G"]) * len(indices["B"])
    for x, y, z in ["RGB", "GBR", "BRG"]:
        cnt -= sum(i + j in doubled_indices[z] for i in indices[x] for j in indices[y])
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict


def main():
    _ = int(input())
    S = input().rstrip()
    indices, doubled_indices = defaultdict(list), defaultdict(set)
    for i, c in enumerate(S):
        indices[c].append(i)
        doubled_indices[c].add(2 * i)
    cnt = len(indices["R"]) * len(indices["G"]) * len(indices["B"])
    for c1, c2, c3 in ["RGB", "GBR", "BRG"]:
        cnt -= sum(
            i + j in doubled_indices[c3] for i in indices[c1] for j in indices[c2]
        )
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict


def main():
    _ = int(input())
    S = input().rstrip()
    indices = defaultdict(set)
    for i, c in enumerate(S):
        indices[c].add(2 * i)
    cnt = len(indices["R"]) * len(indices["G"]) * len(indices["B"])
    for c1, c2, c3 in ["RGB", "GBR", "BRG"]:
        cnt -= sum(
            (i + j) // 2 in indices[c3] for i in indices[c1] for j in indices[c2]
        )
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict


def main():
    _ = int(input())
    S = input().rstrip()
    indices = defaultdict(set)
    for i, c in enumerate(S):
        indices[c].add(i)
    cnt = len(indices["R"]) * len(indices["G"]) * len(indices["B"])
    for c1, c2, c3 in ["RGB", "GBR", "BRG"]:
        cnt -= sum(
            (i + j) % 2 == 0 and (i + j) // 2 in indices[c3]
            for i in indices[c1]
            for j in indices[c2]
        )
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):
def combi_three(n):
    if n < 3:
        return 0
    else:
        return int((n * (n - 1) * (n - 2)) / 6)

def quest_d():
    N = int(input())
    S = input()

    index_dict = {'R':0, 'G':0, 'B':0 }
    for index in range(N):
        index_dict[S[index]] = index_dict[S[index]] + 1

    if index_dict['R'] * index_dict['G'] * index_dict['B'] == 0:
        print(0)
        return

    rrr = combi_three(index_dict['R'])
    ggg = combi_three(index_dict['G'])
    bbb = combi_three(index_dict['B'])
    rrg = combi_three(index_dict['R'] + index_dict['G'])
    ggb = combi_three(index_dict['G'] + index_dict['B'])
    bbr = combi_three(index_dict['B'] + index_dict['R'])

    i = 0
    sum = 0
    while i < N:
        a = S[i]
        j = i
        while j < N - 1:
            j += 1
            if S[j] == a:
                continue

            k = 2 * j - i
            if k < N:
                b = S[j]
                if S[k] != a and S[k] != b:
                    sum += 1
        i += 1
    print(combi_three(N) - sum - rrg - bbr - ggb + rrr + ggg + bbb)

if __name__ == '__main__':
    quest_d()

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

r_num = S.count("R")
g_num = S.count("G")
b_num = S.count("B")

cnt = 0

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if not 2 * j - i < N:
            break
        if S[i] != S[j] and (S[2 * j - i] != S[i] and S[2 * j - i] != S[j]):
            cnt += 1

ans = r_num * g_num * b_num - cnt
print(ans)

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

r_num = S.count("R")
g_num = S.count("G")
b_num = S.count("B")

cnt = 0

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

ans = r_num * g_num * b_num - cnt
print(ans)

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

n = int(input())
s = input().strip()
R = [0] * (n + 1)
G = [0] * (n + 1)
B = [0] * (n + 1)
ans = 0
for i in range(len(s)):
    R[i + 1] = R[i]
    G[i + 1] = G[i]
    B[i + 1] = B[i]
    if s[i] == 'R':
        R[i + 1] += 1
    elif s[i] == 'G':
        G[i + 1] += 1
    else:
        B[i + 1] += 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'R' and s[j] == 'B':
            ans += G[j + 1] - G[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'G':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'R' and s[j] == 'G':
            ans += B[j + 1] - B[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'B':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'B' and s[j] == 'R':
            ans += G[j + 1] - G[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'G':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'B' and s[j] == 'G':
            ans += R[j + 1] - R[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'R':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'G' and s[j] == 'R':
            ans += B[j + 1] - B[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'B':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'G' and s[j] == 'B':
            ans += R[j + 1] - R[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'R':
                ans -= 1


print(ans)

Solution (Python):
n=int(input())
s=input()
ans=0
r=0
g=0
b=0
rl=[]
gl=[]
bl={-2*n}
for k,i in enumerate(s):
    if i=="R":
        r+=1
        rl.append(k)
    elif i=="G":
        g+=1
        gl.append(k)
    else:
        b+=1
        bl|={k}
ans=r*b*g
for i in rl:
    for j in gl:
        if (i+j)/2 in bl:
            ans-=1
        if 2*i-j in bl:
            ans-=1
        if 2*j-i in bl:
            ans-=1
print(ans)

Solution (Python):
import sys

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
S = read_str()

r = 0
g = 0
b = 0
for s in S:
    if s == 'R':
        r += 1
        continue
    if s == 'G':
        g += 1
        continue
    if s == 'B':
        b += 1
        continue

def solve():
    ans = r * g * b
    for d in range(1, N):
        for i in range(0, N - 2 * d):
            if S[i] != S[i + d] and S[i] != S[i + 2 * d] and S[i + d] != S[i + 2 * d]:
                ans -= 1
    return ans

if __name__ == "__main__":
    print(solve())

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

ans = r * g * b

for i in range(n-1):
    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
        ans -= 1

print(ans)

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

cnt = 0
for i in range(N):
    for j in range(1,N):
        l = i - j
        r = i + j
        if (l < 0) or (N-1 < r): break
        if (S[l] != S[i]) and (S[i] != S[r]) and (S[l] != S[r]): cnt += 1

from collections import Counter
c = Counter(S)
if len(c) < 3:
    print(0)
else:
    ans = 1
    for v in c.values(): ans *= v
    print(ans-cnt)

Solution (Python):
def main():
    n = int(input())
    s = input()
    answer = 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 n <= k:
                continue
            elif s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                answer -= 1
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
n = int(input())
s = stdin.readline()
r = 0
g = 0 
b = 0
for i in range(n):
    if s[i] == "R":
        r += 1
    elif s[i] == "G":
        g += 1
    else:
        b += 1
count1 = r*g*b
count2 = 0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1:
            if (s[i] != s[j])and(s[i] != s[2*j-i])and(s[j] != s[2*j-i]):
               count2 += 1
print(count1-count2)

Solution (Python):
n = int(input());S = input()
r,g,b = [],[],[]
for i in range(len(S)):
    if S[i]=='R':r.append(i)
    elif S[i]=='G':g.append(i)
    else:b.append(i)
ans = len(r)*len(g)*len(b)
for r_i in r:
    for g_i in g:
        sho = min(r_i,g_i);dai = max(r_i,g_i)
        
        index = 2*sho-dai
        if index>=0:
            if S[index]=='B':ans-=1
                
        index = (sho+dai)//2
        if (sho+dai)%2 ==0:
            if S[index]=='B':ans-=1
        
        index = 2*dai-sho
        if index<len(S):
            if S[index]=='B':ans-=1
print(ans)

Solution (Python):
from math import gcd

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

r = 0
g = 0
for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
ans = r * g * (n - r - g)

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

Solution (Python):
n = int(input())
s = input()
d = {'R':0, 'G':0, 'B':0}
for x in s:
    d[x] += 1
count1 = d['R']*d['G']*d['B']
maxdif = (n-1)//2
count2 = 0
for d in range(1,maxdif+1):
    for i in range(d,n-d):
        if s[i-d] != s[i] and s[i] != s[i+d] and s[i-d] != s[i+d]:
            count2 += 1
print(count1-count2)

Solution (Python):





N = int(input())
data = input()

out = 0
r=0;g=0;b=0;
for d in data:
    if d =="R":
        r+=1
    elif d=="B":
        b+=1
    else:
        g+=1
out = r*g*b

if N > 2:
    for ii, i in enumerate(data[:-2]):
        for jj,j in enumerate(data[ii+1:-1]):
            jj = jj+ii+1
            if i!=j:
                sum1 = 0                    
                for d in [d for d in "RGB"]:
                    if i!=d and j!=d:
                        let = d

                if jj+jj-ii<N:
                    if data[jj+jj-ii]==let:                    
                        out-=1
print(out)

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

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

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

print(r_count*g_count*b_count-discount)

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
p=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 and s[i]!=s[k] and s[j]!=s[k]:
      p-=1
print(p)

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

Solution (Python):
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
c = 0
for i in range(N-2):
  for j in range(i+1,(i+N+1)//2):
    k = j*2-i
    if len(set([S[i], S[j], S[k]]))==3:
      c += 1
print(r*g*b-c)

Solution (Python):
N=int(input())
S=input()
totalPattern = S.count("R")*S.count("G")*S.count("B")
for i in range(N-1):
    for j in range(i+1,N):
        k=j+(j-i)
        if k>N-1:
            continue
        if not S[i] == S[j] and not S[i] == S[k] and not S[j] == S[k]:
            totalPattern-=1
print(totalPattern)

Solution (Python):
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
c = 0
for i in range(N-2):
  for j in range(i+1,(i+N+1)//2):
    k = j*2-i
    if len(set([S[i], S[j], S[k]]))==3:
      c += 1
print(r*g*b-c)

Solution (Python):
N = int(input())
S = input()
a = []
for i in range(len(S)):
  if S[i] == 'R':
    a.append(0)
  elif S[i] == 'G':
    a.append(1)
  elif S[i] == 'B':
    a.append(2)
    
cnt = [0] * 3
for i in range(N):
  cnt[a[i]] += 1
ans = cnt[0] * cnt[1] * cnt[2]

for j in range(N):
  for i in range(j):
    k = j + (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):
def main():
    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(N-2):
        for j in range(i,N-1):
            k = j + (j - i)
            if k < N:
                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()
R = S.count("R")
G = S.count("G")
B = S.count("B")
cnt = 0
for i in range(N):
    for j in range(i+1,N):
        if j*2-i < N:
            if (S[i] != S[j]) and (S[i] !=S[(j*2)-i]) and (S[j] != S[(j*2)-i]):
                cnt += 1
            else:
                pass
        else:
            pass
print(R*G*B - cnt)

Solution (Python):
N=int(input())
S=list(input())
a=[0]*4

for i in range(N):
  if S[i]=='R':a[0]+=1
  if S[i]=='G':a[1]+=1
  if S[i]=='B':a[2]+=1
    
for i in range(N):
  for j in range(i+1,N):
    k=j+(j-i)
    if k>=N:
      break
    else:
      if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i] and j-i==k-j:
        a[3]+=1

print(a[0]*a[1]*a[2]-a[3])

Solution (Python):
n=int(input())
s=str(input())
r,g,b=0,0,0
ans=0
for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    else:
        b += 1
for i in range(0,n):
    if s[i]=='R':
        ans += b*g
        r -= 1
    elif s[i]=='G':
        ans += r*b
        g -= 1
    else:
        ans += r*g
        b -= 1
for i in range(1,n):
    for j in range(n):
        l,mid,r=j,j+i,j+i+i
        if r<n:
            d={}
            d[s[l]]=1
            d[s[mid]]=1
            d[s[r]]=1
            if len(d)==3:
                ans -= 1
print(ans)

Solution (Python):
N=int(input())
S=input()
R,G,B=0,0,0
ans=0
for i in range(N):
    if S[i]=='R':
        R+=1
    elif S[i]=='G':
        G+=1
    else:
        B+=1
ans+=R*G*B
f,s,t=0,1,2
while t<=N-1:
    i=0
    while i+t<=N-1:
        if S[f+i]!=S[s+i] and S[f+i]!=S[t+i] and S[s+i]!=S[t+i]:
            ans-=1
        i+=1
    s+=1
    t+=2
print(ans)

Solution (Python):
N=int(input())
S=input()
R,G,B=0,0,0
ans=0
for i in range(N):
    if S[i]=='R':
        R+=1
    elif S[i]=='G':
        G+=1
    else:
        B+=1
ans+=R*G*B
f,s,t=0,1,2
while t<=N-1:
    i=0
    while i+t<=N-1:
        if S[f+i]!=S[s+i] and S[f+i]!=S[t+i] and S[s+i]!=S[t+i]:
            ans-=1
        i+=1
    s+=1
    t+=2
print(ans)

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

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

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

ans = r_count * g_count * b_count

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

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

print(ans)

Solution (Python):
import sys
import os
from io import BytesIO, IOBase






'''def sieve(n):
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * p, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r'''


'''def divs(n, start=1):
    divisors = []
    for i in range(start, int(n**.5) + 1):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return len(divisors)'''


'''def divn(n, primes):
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t
    return divs_number'''


'''def flin(d, x, default=-1):
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return (default, default)
    else:
        return (left, right)'''


'''def xor1_n(n):
    d={0:n,1:1,2:n+1,3:0}
    return d[n&3]'''

def cel(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def prr(a, sep=' '): print(sep.join(map(str, a)))
def dd(): return defaultdict(int)
def ddl(): return defaultdict(list)
def ddd(): return defaultdict(defaultdict(int))


 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
	newlines = 0
 
	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None
 
	def read(self):
			while True:
					b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
					if not b:
							break
					ptr = self.buffer.tell()
					self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
			self.newlines = 0
			return self.buffer.read()
 
	def readline(self):
			while self.newlines == 0:
					b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
					self.newlines = b.count(b"\n") + (not b)
					ptr = self.buffer.tell()
					self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
			self.newlines -= 1
			return self.buffer.readline()
 
	def flush(self):
			if self.writable:
					os.write(self._fd, self.buffer.getvalue())
					self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
	def __init__(self, file):
			self.buffer = FastIO(file)
			self.flush = self.buffer.flush
			self.writable = self.buffer.writable
			self.write = lambda s: self.buffer.write(s.encode("ascii"))
			self.read = lambda: self.buffer.read().decode("ascii")
			self.readline = lambda: self.buffer.readline().decode("ascii")
 

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

from collections import defaultdict 


















n=ii()
s=input()
r,g=[],[]
B=dd()
for i in range(n):
    if s[i]=='R':
        r.append(i+1)
    elif s[i]=='G':
        g.append(i+1)
    else:
        B[i+1]=True
ans=len(r)*len(g)*len(B)
for a in r:
    for b in g:
        if (a+b)&1==0 and B[(a+b)//2]:
            ans-=1
        if B[2*b-a]:
            ans-=1
        if B[2*a-b]:
            ans-=1
print(ans)

Solution (Python):
n=int(input())
s=str(input())
R=[]
B=[]
G=[]
for i in range(n):
  if s[i]=='R':
    R.append(i)
  elif s[i]=='B':
    B.append(i)
  else:
    G.append(i)
ans=len(R)*len(B)*len(G)

for i in range(n-1):
  for j in range(i+1,n):
    char1=s[i]
    char2=s[j]
    if char1==char2:
      pass
    else:
      if n>2*j-i>j and s[2*j-i]!=char1 and s[2*j-i]!=char2:
        ans-=1
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 diff in range(n):
    j = i + diff
    k = j + diff
    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 = [0 for i in range(n+1)]
G = [0 for i in range(n+1)]
B = [0 for i in range(n+1)]
R[n] = 0
G[n] = 0
B[n] = 0

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

sm = 0
for i in range(n):
    for j in range(i+1,n):
        nxt = j-i + j
        f = True
        if nxt >= n:
            f = False
        if s[i] == s[j]:
            continue
        if s[i] != 'R' and s[j] != 'R':
            sm += R[j+1]
            if f:
                sm -= (s[nxt] == 'R')
        if s[i] != 'G' and s[j] != 'G':
            sm += G[j + 1]
            if f:
                sm -= (s[nxt] == 'G')
        if s[i] != 'B' and s[j] != 'B':
            sm += B[j + 1]
            if f:
                sm -= (s[nxt] == 'B')
print(sm)

Solution (Python):
from functools import reduce
from operator import mul
N = int(input())
S = input()
appearances = {"R": 0, "G": 0, "B": 0}
for c in S:
    appearances[c] += 1
print(reduce(mul, list(appearances.values())) - sum(sum(len(set(S[start] + S[start + step] + S[start + step * 2])) == 3 for start in range(N - step * 2)) for step in range(1, N // 2 + 1)))

Solution (Python):
from numpy import prod
N = int(input())
S = input()
appearances = {"R": 0, "G": 0, "B": 0}
for c in S:
    appearances[c] += 1
print(prod(list(appearances.values())) - sum(sum(len(set(S[start] + S[start + step] + S[start + step * 2])) == 3 for start in range(N - step * 2)) for step in range(1, N // 2 + 1)))

Solution (Python):
def main():
    N = int(input())
    S = list(input())
    R = []
    G = []
    B = []
    Reval = {}
    Geval = {}
    Beval = {}
    for i in range(N):
        if S[i] == "R":
            R.append(i)
            Reval[2*i] = 2*i
        elif S[i] == "G":
            G.append(i)
            Geval[2*i] = 2*i
        elif S[i] == "B":
            B.append(i)
            Beval[2*i] = 2*i
    count = len(R)*len(G)*len(B)

    for a in R:
        for b in G:
            if (a + b) in Beval:
                count -= 1
    for a in G:
        for b in B:
            if (a + b) in Reval:
                count -= 1
    for a in B:
        for b in R:
            if (a + b) in Geval:
                count -= 1
    print(count)

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N = int(input())
    S = list(input())
    R = []
    G = []
    B = []
    Reval = {}
    Geval = {}
    Beval = {}
    for i in range(N):
        if S[i] == "R":
            R.append(i)
            Reval[2*i] = 2*i
        elif S[i] == "G":
            G.append(i)
            Geval[2*i] = 2*i
        elif S[i] == "B":
            B.append(i)
            Beval[2*i] = 2*i
    count = len(R)*len(G)*len(B)

    for a in R:
        for b in G:
            if (a + b) in Beval:
                count -= 1
    for a in G:
        for b in B:
            if (a + b) in Reval:
                count -= 1
    for a in B:
        for b in R:
            if (a + b) in Geval:
                count -= 1
    print(count)

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
S = list(input())
R = []
G = []
B = []
Reval = {}
Geval = {}
Beval = {}
for i in range(N):
    if S[i] == "R":
        R.append(i)
        Reval[2*i] = 2*i
    elif S[i] == "G":
        G.append(i)
        Geval[2*i] = 2*i
    elif S[i] == "B":
        B.append(i)
        Beval[2*i] = 2*i
count = len(R)*len(G)*len(B)

for a in R:
    for b in G:
        if (a + b) in Beval:
            count -= 1
for a in G:
    for b in B:
        if (a + b) in Reval:
            count -= 1
for a in B:
    for b in R:
        if (a + b) in Geval:
            count -= 1
print(count)

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

N = int(input())
I = input()

R =0
G =0
B =0
for i in range(N):
    if (I[i] == 'R'):
        R+=1
    if (I[i] == 'G'):
        G+=1
    if (I[i] == 'B'):
        B+=1


sum = R * G * B

for i in range(N):
    for j in range(i+1,N):
        if I[i] != I[j]:
            k = j + j - i
            if k >= N:
                continue
            if I[i] != I[k] and I[j] != I[k]:
                sum -= 1

print(sum)

Solution (Python):
N = int(input())
S = list(map(str,input()))

r = 0
g = 0
b = 0
for i in range(N):
    if S[i] == "R":
        r+=1
    elif S[i] == "G":
        g+=1
    else:
        b+=1
ans = 0
for i in range(N):
    for j in range(1,N//2+2):
        if i+2*j < N:
            if S[i] != S[i+j] and S[i] != S[i+2*j] and S[i+j] != S[i+2*j]:
                ans +=1

print(r*g*b-ans)

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

r, g, b = 0, 0, 0
for i in range(n):  
    if S[i] == 'R':
        r += 1
    elif S[i] == 'G':
        g += 1
    elif S[i] == 'B':
        b += 1
    else:
        pass

ans = r * g * b
for i in range(1, n-1): 
    d = i
    if d > n-1 - d:
        d = n-1 - d
    else:
        pass
    for j in range(1, d+1): 
        if S[i] != S[i-j] and S[i] != S[i+j] and S[i-j] != S[i+j]:
            ans -= 1
        else:
            pass
print(ans)