AtCoder Beginner Contest 162 - RGB Triplets

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-----
4
RRGB

-----Sample Output-----
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.

Source: atcoder | Difficulty: MEDIUM_HARD


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


n = int(input())
s = input()
sum = s.count('R') * s.count('G') * s.count('B')
for i in range(1, n - 1):
	for j in range(i + 1, n):
		k = 2 * j - i - 1
		if k >= n:
			break
		if len({s[i - 1], s[j - 1], s[k]}) == 3:
			sum -= 1
print(sum)


n = int(input())
s = input()
cnt = 0
for i in range(n):
	for j in range(i + 1, n):
		k = 2 * j - i
		if j < k < n and len({s[i], s[j], s[k]}) == 3:
			cnt += 1
print(s.count('R') * s.count('G') * s.count('B') - cnt)


import collections
n = int(input())
s = input()
c = collections.Counter(s)
ans = c['R'] * c['G'] * c['B']
for i in range(1, (n - 1) // 2 + 1):
	for k in range(n - i * 2):
		if s[k] != s[k + i] and s[k] != s[k + 2 * i] and (s[k + i] != s[k + 2 * i]):
			ans -= 1
print(ans)


def main():
	N = int(input())
	S = input()
	RGBdict = {'R': 0, 'G': 0, 'B': 0}
	for i in range(N):
		RGBdict[S[i]] += 1
	if N % 2 == 0:
		maxsa = N // 2 - 1
	else:
		maxsa = N // 2
	tmp = N - 2
	cnt = 0
	for j in range(maxsa + 1):
		num = tmp - j * 2
		for k in range(num):
			if S[N - num + k] != S[N - num + k - j - 1] and S[N - num + k] != S[N - num + k - 2 * j - 2] and (S[N - num + k - j - 1] != S[N - num + k - 2 * j - 2]):
				cnt += 1
	ans = RGBdict['R'] * RGBdict['G'] * RGBdict['B']
	ans -= cnt
	return ans
print(main())


n = int(input())
s = input()
ans = s.count('R') * s.count('G') * s.count('B')
for i in range(n):
	for j in range(i, n):
		if 2 * j - i >= n:
			break
		elif s[i] != s[j] != s[2 * j - i] != s[i]:
			ans -= 1
print(ans)


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


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


from collections import Counter
N = int(input())
S = input()
D = Counter(S)
ans = 1
for d in D.values():
	ans *= d
if len(D.values()) < 3:
	ans = 0
for i in range(N):
	for x in range(1, (N - i + 1) // 2):
		if S[i] != S[i + x] and S[i + x] != S[i + 2 * x] and (S[i + 2 * x] != S[i]):
			ans -= 1
print(ans)


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


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 zip(S, S[i:], S[2 * i:]):
		if len(set(j)) == 3:
			ans -= 1
print(ans)


import math
import sys
import os
from operator import mul
import bisect
sys.setrecursionlimit(10 ** 7)

def _S():
	return sys.stdin.readline().rstrip()

def I():
	return int(_S())

def LS():
	return list(_S().split())

def LI():
	return list(map(int, LS()))
if os.getenv('LOCAL'):
	inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0] + '.txt'
	sys.stdin = open(inputFile, 'r')
INF = float('inf')
N = I()
S = list(_S())
ans = 0
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)
ans = len(r) * len(g) * len(b)
for i in range(N):
	for j in range(i, N):
		k = j + (j - i)
		if k > N - 1:
			break
		if S[i] == S[j]:
			continue
		if S[i] == S[k] or S[j] == S[k]:
			continue
		ans -= 1
print(ans)


n = int(input())
s = input()
count = {'R': 0, 'G': 0, 'B': 0}
for c in s:
	count[c] += 1
(r, g, b) = count.values()
ans = r * g * b
for i in range(1, (n - 1) // 2 + 1):
	for rgb in zip(s, s[i:], s[i * 2:]):
		if len(set(rgb)) == 3:
			ans -= 1
print(ans)


N = int(input())
S = input()
a = S.count('R')
b = S.count('G')
c = S.count('B')
ans = a * b * c
for i in range(N - 2):
	for j in range(i + 1, N - 1):
		if S[i] != S[j]:
			if 2 * j - i <= N - 1 and S[i] != S[2 * j - i] != S[j]:
				ans -= 1
print(ans)


N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
	for j in range(i + 1, N):
		if (i + j) % 2:
			continue
		k = (i + j) // 2
		if len(set([S[i], S[k], S[j]])) == 3:
			ans -= 1
print(ans)


n = int(input())
s = list(input())
a = s.count('R')
b = s.count('G')
c = s.count('B')
ans = 0
for i in range(n - 2):
	for j in range(i + 1, n - 1):
		k = 2 * j - i
		if n - 1 >= k and (s[i] != s[j] and s[i] != s[k] and (s[j] != s[k])):
			ans = ans + 1
print(a * b * c - ans)


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


n = int(input())
s = input()
c = s.count
print(c('R') * c('G') * c('B') - sum((len(set(s[i:j + 1:j - i >> 1])) > 2 for i in range(n - 2) for j in range(i + 2, n, 2))))


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):
	j = 0
	while j + 2 * i <= n - 1:
		if len(set(s[j] + s[j + i] + s[j + 2 * i])) == 3:
			ans -= 1
		j += 1
print(ans)


N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
minus = 0
for i in range(0, N):
	j = 0
	while j + 2 * i < N:
		if S[j] != S[j + i] and S[j + i] != S[j + 2 * i] and (S[j] != S[j + 2 * i]):
			minus += 1
		j += 1
print(ans - minus)


(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]))))


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


n = int(input())
s = input()
D = {'R': [], 'G': [], 'B': []}
for i in range(1, n + 1):
	D[s[i - 1]].append(i)
ans = len(D['R']) * len(D['G']) * len(D['B'])
count = 0
for i in range(0, n):
	for j in range(i + 1, n):
		k = 2 * j - i
		if k >= n:
			break
		if s[i] != s[j] and s[j] != s[k] and (s[i] != s[k]):
			count += 1
print(ans - count)


n = int(input())
s = list(input())
count = s.count('R') * s.count('G') * s.count('B')
for i in range(n - 2):
	for j in range(1, (n + 1 - i) // 2):
		if sorted([s[i], s[i + j], s[i + 2 * j]]) == ['B', 'G', 'R']:
			count -= 1
print(count)


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


N = int(input())
S = input()
all = S.count('R') * S.count('G') * S.count('B')
cnt = 0
for i in range(N):
	for j in range(i, N):
		k = 2 * j - i
		if k >= N:
			continue
		if S[i] != S[j] and S[i] != S[k] and (S[j] != S[k]):
			cnt += 1
print(all - cnt)


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


def main():
	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):
			k = j + j - i
			if k >= n:
				break
			if s[i] != s[j] and s[j] != s[k] and (s[k] != s[i]):
				cnt += 1
	ans = r * g * b - cnt
	print(ans)

def __starting_point():
	main()
__starting_point()


import itertools
N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
c = list(itertools.permutations(['R', 'B', 'G'], 3))
for j in range(1, N):
	for i in range(N - j * 2):
		if (S[i], S[i + j], S[i + j * 2]) in c:
			ans -= 1
print(ans)


import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp
INF = inf
MOD = 1000000007
n = int(input())
s = input()
tmp = 0
res = s.count('R') * s.count('G') * s.count('B')
for i in range(n - 2):
	for j in range(i + 1, floor((n + i + 1) / 2)):
		k = 2 * j - i
		if s[i] != s[j] and s[j] != s[k] and (s[k] != s[i]):
			res -= 1
print(res)


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 not (S[j] == S[j + i] or S[j] == S[j + i * 2] or S[j + i] == S[j + i * 2]):
			ans -= 1
print(ans)


(N, S) = (int(input()), input())
(r, g, b) = (0, 0, 0)
for i in list(S):
	if i == 'R':
		r += 1
	if i == 'G':
		g += 1
	if i == 'B':
		b += 1
ans = r * g * b
for i in range(N - 2):
	for j in range(i + 1, N - 1):
		if j - i > N - j - 1:
			break
		if S[i] != S[j] and S[j] != S[2 * j - i] and (S[2 * j - i] != S[i]):
			ans -= 1
print(ans)


N = int(input())
S = input()
C = set('RGB')
L = [{c: 0 for c in C}]
M = L[:]
z = -sum((C == set(S[j] + S[j + r] + S[j + 2 * r]) for r in range(1, (N + 1) // 2) for j in range(N - 2 * r)))
for c in S:
	L += [L[-1].copy()]
	L[-1][c] += 1
for c in reversed(S):
	M += [M[-1].copy()]
	M[-1][c] += 1
for (c, l, m) in zip(S, L, reversed(M[:-1])):
	(a, b) = C - set(c)
	z += l[a] * m[b] + l[b] * m[a]
print(z)


import collections
N = int(input())
S = list(input())
C = collections.Counter(S)
ans = C['R'] * C['G'] * C['B']
for i in range(1, N - 1):
	b = S[i]
	ans -= sum((a != b and b != c and (c != a) for (a, c) in zip(S[i - 1::-1], S[i + 1:])))
print(ans)


def main():
	N = int(input())
	S = list(input())
	R = []
	G = []
	B = []
	cnt = 0
	for i in range(len(S)):
		if S[i] == 'R':
			R.append(i)
		elif S[i] == 'G':
			G.append(i)
		elif S[i] == 'B':
			B.append(i)
	l = len(B)
	for i in range(len(R)):
		r = R[i]
		for j in range(len(G)):
			g = G[j]
			cnt += l
			if g > r:
				M = g
				m = r
			elif r > g:
				M = r
				m = g
			d = M - m
			if M + d < N and S[M + d] == 'B':
				cnt -= 1
			if m - d >= 0 and S[m - d] == 'B':
				cnt -= 1
			if (M + m) % 2 == 0 and S[int((M + m) / 2)] == 'B':
				cnt -= 1
	print(cnt)
main()


import sys
read = sys.stdin.read
readlines = sys.stdin.readlines

def main():
	n = int(input())
	s = input()
	res = s.count('R') * s.count('G') * s.count('B')
	if res == 0:
		print(0)
		return
	for i1 in range(n):
		for i2 in range(i1 + 1, n):
			i3 = i2 * 2 - i1
			if i3 <= n - 1:
				if s[i1] != s[i2] and s[i1] != s[i3] and (s[i2] != s[i3]):
					res -= 1
	print(res)

def __starting_point():
	main()
__starting_point()


N = int(input())
S = list(input())
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r * g * b
for j in range(1, N):
	t = 1
	while j - t >= 0 and j + t <= N - 1:
		if S[j - t] != S[j] and S[j] != S[j + t] and (S[j + t] != S[j - t]):
			ans -= 1
		t += 1
print(ans)
return


n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = n - r - g
ans = r * g * b
for i in range(1, n):
	for j in range(n):
		if j + i * 2 >= n:
			break
		(d1, d2, d3) = (s[j], s[j + i], s[j + i * 2])
		if not (d1 == d2 or d2 == d3 or d1 == d3):
			ans -= 1
print(ans)


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


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


n = int(input())
a = input()
c = 0
for i in range(1, n // 2 + 1):
	for j in range(n - 2 * i):
		if a[j] != a[j + i] and a[j + i] != a[j + 2 * i] and (a[j] != a[j + 2 * i]):
			c += 1
ans = a.count('R') * a.count('G') * a.count('B') - c
print(ans)


N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
	for j in range(i + 1, N):
		k = j - i + j
		if k < N:
			if {S[i], S[j], S[k]} == {'R', 'G', 'B'}:
				ans -= 1
print(ans)


def main():
	n = int(input())
	s = input()
	cnt = [0] * 3
	ans = 1
	for i in s:
		if i == 'R':
			cnt[0] += 1
		elif i == 'G':
			cnt[1] += 1
		else:
			cnt[2] += 1
	for i in cnt:
		ans *= i
	for i in range(n):
		for j in range(i + 1, n):
			k = j + (j - i)
			if k < n:
				if s[i] == s[j]:
					continue
				if s[j] == s[k]:
					continue
				if s[k] == s[i]:
					continue
				ans -= 1
	print(ans)

def __starting_point():
	main()
__starting_point()


from collections import *
N = int(input())
S = input()
c = Counter(S)
ans = c['R'] * c['G'] * c['B']
for i in range(1, N // 2 + 1):
	for j in range(i, N - i):
		if S[j - i] != S[j] and S[j] != S[j + i] and (S[j + i] != S[j - i]):
			ans -= 1
print(ans)


N = int(input())
S = input()
from collections import Counter
x = list(Counter(S).values())
if len(x) < 3:
	print(0)
	return
c = 0
for i in range(N):
	j = i + 1
	for j in range(i + 1, N):
		if S[i] != S[j]:
			k = 2 * j - i
			if k < N and (S[k] != S[i] and S[k] != S[j]):
				c += 1
print(x[0] * x[1] * x[2] - c)


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
ans = r * g * b
for i in range(n):
	for j in range(i + 1, n):
		k = j + j - i
		if k < n:
			if s[i] != s[j] and s[j] != s[k] and (s[k] != s[i]):
				ans -= 1
print(ans)


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


N = int(input())
s = input()
all = s.count('R') * s.count('G') * s.count('B')
a = 0
for i in range(N):
	for j in range(1, (N - i) // 2 + 1):
		if i + 2 * j >= N:
			break
		if s[i] != s[i + j] and s[i] != s[i + 2 * j] and (s[i + j] != s[i + 2 * j]):
			a += 1
print(all - a)


N = int(input())
S = input()
(ans, gap) = (S.count('R') * S.count('G') * S.count('B'), 1)
while gap * 2 + 1 <= N:
	ans -= sum([1 for i in range(N - gap * 2) if {'R', 'G', 'B'} == {S[i], S[i + gap], S[i + 2 * gap]}])
	gap += 1
print(ans)


N = int(input())
S = input()
lsS = list(S)
lscount = [lsS.count('R'), lsS.count('G'), lsS.count('B')]
ans = 1
for i in lscount:
	ans *= i
for i in range(N - 2):
	for j in range(i + 1, N - 1):
		if lsS[i] == lsS[j]:
			continue
		if 2 * j - i <= N - 1:
			if lsS[i] != lsS[2 * j - i] and lsS[j] != lsS[2 * j - i]:
				ans -= 1
		else:
			break
print(ans)


N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r * g * b
for i in range(N - 2):
	for j in range(i + 1, N - 1):
		if 2 * j - i >= N:
			break
		elif S[i] != S[j] and S[j] != S[2 * j - i] and (S[2 * j - i] != S[i]):
			ans -= 1
print(ans)


n = int(input())
s = input()
cnt = s.count('R') * s.count('G') * s.count('B')
for i in range(n - 2):
	pl = 1
	while i + pl * 2 < n:
		if s[i] != s[i + pl] and s[i + pl] != s[i + pl * 2] and (s[i] != s[i + pl * 2]):
			cnt -= 1
		pl += 1
print(cnt)


N = int(input())
S = input()
ris = [i for (i, s) in enumerate(S) if s == 'R']
gis = [i for (i, s) in enumerate(S) if s == 'G']
bis = [i for (i, s) in enumerate(S) if s == 'B']
all = len(ris) * len(gis) * len(bis)
cnt = 0
for i in range(N):
	for j in range(i + 1, N):
		k = 2 * j - i
		if 0 <= k < N:
			if S[i] != S[j] and S[i] != S[k] and (S[j] != S[k]):
				cnt += 1
ans = all - cnt
print(ans)


N = int(input())
S = list(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):
		k = j * 2 - i
		if k >= N:
			break
		if S[i] != S[j] and S[i] != S[k] and (S[j] != S[k]):
			ans -= 1
print(ans)


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


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):
	for j in range(i + 1, n):
		if s[i] == s[j]:
			continue
		k = 2 * j - i
		if k >= n or s[k] == s[i] or s[k] == s[j]:
			continue
		ans -= 1
print(ans)


N = int(input())
S = input()
uns = 0
for d in range(1, (N - 1) // 2 + 1):
	for i in range(N - 2 * d):
		(s, t, u) = (S[i], S[i + d], S[i + 2 * d])
		if s != t and t != u and (u != s):
			uns += 1
r = S.count('R')
g = S.count('G')
b = N - r - g
ans = r * g * b - uns
print(ans)


N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
tmp = 0
for i in range(N):
	for j in range(i, N):
		if j - i + j < N:
			if S[i] != S[j] and S[j] != S[j - i + j] and (S[i] != S[j - i + j]):
				tmp += 1
ans = ans - tmp
print(ans)


e = enumerate
(_, s) = open(0)
(r, g, b) = map(s.count, 'RGB')
print(r * g * b - sum((c != d != s[i + j] != c for (i, c) in e(s) for (j, d) in e(s[i:-1:2]))))


N = int(input())
S = input()
R = []
G = []
B = []
for i in range(N):
	if S[i] == 'R':
		R.append(i + 1)
	elif S[i] == 'G':
		G.append(i + 1)
	elif S[i] == 'B':
		B.append(i + 1)
lenb = len(B)
cnt = 0
for r in R:
	for g in G:
		up = max(r, g)
		down = min(r, g)
		diff = up - down
		chk = 0
		if up + diff <= N:
			if S[up + diff - 1] == 'B':
				chk += 1
		if down - diff >= 1:
			if S[down - diff - 1] == 'B':
				chk += 1
		if diff % 2 == 0:
			if S[int(up - diff / 2 - 1)] == 'B':
				chk += 1
		cnt += lenb - chk
print(cnt)


N = int(input())
S = input()
bad_list = ['RGB', 'RBG', 'GRB', 'GBR', 'BRG', 'BGR']
res = S.count('R') * S.count('G') * S.count('B')
for (i, s) in enumerate(S):
	for j in range((i + 1) // 2 + 1)[1:]:
		if S[i::-j][:3] in bad_list:
			res -= 1
print(res)


N = int(input())
S = input()
SS = []
for i in range(N):
	if S[i] == 'R':
		SS.append(1)
	elif S[i] == 'G':
		SS.append(2)
	else:
		SS.append(4)
s = 0
for i in range(1, N - 1):
	for j in range(1, min(i + 1, N - i)):
		if SS[i - j] + SS[i] + SS[i + j] == 7:
			s += 1
print(SS.count(1) * SS.count(2) * SS.count(4) - s)


import math
n = int(input())
s = input()
ex = 0
for i in range(n - 2):
	for x in range(1, int(math.ceil((n - i) / 2))):
		if s[i] != s[i + x] and s[i] != s[i + 2 * x] and (s[i + x] != s[i + 2 * x]):
			ex += 1
print(s.count('R') * s.count('G') * s.count('B') - ex)


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


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


N = int(input())
S = list(input())
i = 1
count = 0
while i * 2 < N:
	n = 0
	while n + i * 2 < N:
		if S[n] != S[n + i] and S[n + i] != S[n + 2 * i] and (S[n] != S[n + i * 2]):
			count += 1
		n += 1
	i += 1
print(S.count('G') * S.count('R') * S.count('B') - count)


N = int(input())
S = input()
C = {'R', 'G', 'B'}
cumsum = {c: [0 for i in range(N)] for c in C}
for i in range(N):
	s = S[i]
	cumsum[s][i] = 1
for i in range(2, N + 1):
	for c in C:
		cumsum[c][-i] = cumsum[c][-i] + cumsum[c][-(i - 1)]
ans = 0
for i in range(N - 2):
	s = S[i]
	(a, b) = C - {s}
	ans += cumsum[a][i] * cumsum[b][i]
for i in range(1, (N - 1) // 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]):
			ans -= 1
print(ans)


from collections import Counter

def main():
	n = int(input())
	s = input()
	d = Counter(s)
	a = d['R'] * d['G'] * d['B']
	for i in range(n):
		t = min(n - i - 1, i)
		for j in range(t + 1):
			if s[i - j] != s[i] and s[i - j] != s[i + j] and (s[i] != s[i + j]):
				a -= 1
	print(a)

def __starting_point():
	main()
__starting_point()


N = int(input())
S = input()
R_list = list()
G_list = list()
B_list = list()
for i in range(N):
	if S[i] == 'R':
		R_list.append(i)
	elif S[i] == 'G':
		G_list.append(i)
	else:
		B_list.append(i)
ans = len(R_list) * len(G_list) * len(B_list)
for r in R_list:
	for g in G_list:
		up = max(r, g)
		down = min(r, g)
		diff = up - down
		if up + diff < N and S[up + diff] == 'B':
			ans -= 1
		if down - diff >= 0 and S[down - diff] == 'B':
			ans -= 1
		if diff % 2 == 0 and S[down + diff // 2] == 'B':
			ans -= 1
print(ans)


n = int(input())
s = input()
x = s.count('R') * s.count('G') * s.count('B')
for i in range(1, (n - 1) // 2 + 1):
	for j in range(n - i * 2):
		if len(set([s[j], s[j + i], s[j + i + i]])) == 3:
			x -= 1
print(x)


n = int(input())
s = input()
c = s.count
print(c('R') * c('G') * c('B') - sum((sorted(s[i:j + 1:(j - i) // 2]) == list('BGR') for i in range(n - 2) for j in range(i + 2, n, 2))))


def main():
	n = int(input())
	s = input()
	sm = s.count('R') * s.count('G') * s.count('B')
	for i in range(1, (n - 1) // 2 + 1):
		for j in range(n - 2 * i):
			if len(set(s[j] + s[j + i] + s[j + 2 * i])) == 3:
				sm -= 1
	print(sm)
main()


N = int(input())
S = input()
from collections import Counter
C = Counter(S)
ans = C['R'] * C['G'] * C['B']
for i in range(N):
	for j in range(i + 1, N):
		if j + (j - i) >= N:
			break
		if S[i] != S[j] != S[j + (j - i)] != S[i]:
			ans -= 1
print(ans)


n = int(input())
S = list(input())
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(n - 2):
	for j in range(i + 1, (n + i + 1) // 2):
		if S[i] != S[j] and S[j] != S[2 * j - i] and (S[2 * j - i] != S[i]):
			ans -= 1
print(ans)


N = int(input())
S = list(input())
R = []
G = []
for i in range(N):
	if S[i] == 'R':
		R.append(i)
	elif S[i] == 'G':
		G.append(i)
r = len(R)
g = len(G)
ans = r * g * (N - r - g)
for i in R:
	for j in G:
		a = 2 * i - j
		b = 2 * j - i
		c = i + j
		if 0 <= a < N and S[a] == 'B':
			ans -= 1
		if 0 <= b < N and S[b] == 'B':
			ans -= 1
		if c % 2 == 0 and S[c // 2] == 'B':
			ans -= 1
print(ans)


N = int(input())
S = input()
cnt = 0
for i in range(N):
	left = i - 1
	right = i + 1
	while 0 <= left and right < N:
		if S[i] != S[left] and S[i] != S[right] and (S[left] != S[right]):
			cnt += 1
		left -= 1
		right += 1
x = S.count('R')
y = S.count('G')
z = S.count('B')
print(x * y * z - cnt)


import math
from math import gcd, pi
INF = float('inf')
import sys
sys.setrecursionlimit(10 ** 6)
import itertools
from collections import Counter, deque

def i_input():
	return int(input())

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

def i_list():
	return list(i_map())

def i_row(N):
	return [i_input() for _ in range(N)]

def i_row_list(N):
	return [i_list() for _ in range(N)]

def s_input():
	return input()

def s_map():
	return input().split()

def s_list():
	return list(s_map())

def s_row(N):
	return [s_input for _ in range(N)]

def s_row_str(N):
	return [s_list() for _ in range(N)]

def s_row_list(N):
	return [list(s_input()) for _ in range(N)]

def main():
	n = i_input()
	s = s_input()
	r_cn = s.count('R')
	g_cn = s.count('G')
	b_cn = s.count('B')
	ans = r_cn * g_cn * b_cn
	for i in range(n):
		for d in range(n):
			j = i + d
			k = j + d
			if k >= n:
				break
			if s[i] != s[j] and s[j] != s[k] and (s[k] != s[i]):
				ans -= 1
	print(ans)

def __starting_point():
	main()
__starting_point()


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, bisect
import sys
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
stdin = sys.stdin
ni = lambda : int(ns())
nf = lambda : float(ns())
na = lambda : list(map(int, stdin.readline().split()))
nb = lambda : list(map(float, stdin.readline().split()))
ns = lambda : stdin.readline().rstrip()
N = ni()
S = ns()
d = {'R': 0, 'G': 0, 'B': 0}
for i in range(N):
	d[S[i]] += 1
ans = 1
for v in d.values():
	ans *= v
for i in range(N):
	for j in range(i + 1, N):
		k = j + (j - i)
		if k >= N:
			break
		if S[i] != S[j] and S[j] != S[k] and (S[i] != S[k]):
			ans -= 1
print(max(ans, 0))


N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
	a = S[i]
	for j in range(i + 2, N, 2):
		b = S[j]
		k = (i + j) // 2
		if sorted([a, S[k], b]) == ['B', 'G', 'R']:
			ans -= 1
print(ans)


N = int(input())
S = str(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]:
			continue
		if 2 * j - i < N:
			if S[2 * j - i] != S[i] and S[2 * j - i] != S[j]:
				ans -= 1
print(ans)


N = int(input())
S = input().rstrip()
t = S.count('R') * S.count('G') * S.count('B')
d = 0
for i in range(N):
	for j in range(i + 1, N):
		if S[i] == S[j]:
			continue
		k = 2 * j - i
		if k >= N or S[i] == S[k] or S[j] == S[k]:
			continue
		d += 1
print(t - d)


(n, s) = open(0)
n = int(n)
c = s.count
print(c('R') * c('G') * c('B') - sum((s[i] != s[j] != s[i + j >> 1] != s[i] for i in range(n) for j in range(i + 2, n, 2))))


N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N - 2):
	for d in range(1, (N - i - 1) // 2 + 1):
		if {S[i], S[i + d], S[i + 2 * d]} == {'R', 'G', 'B'}:
			ans -= 1
print(ans)


n = int(input())
s = list(input())
cnt = 0
cnt += s.count('R') * s.count('G') * s.count('B')
for i in range(1, n - 1):
	for j in range(1, min(i, n - i - 1) + 1):
		if s[i - j] != s[i] and s[i] != s[i + j] and (s[i + j] != s[i - j]):
			cnt -= 1
print(cnt)


n = int(input())
s = input()
(r, g, b) = (s.count('R'), s.count('G'), s.count('B'))
cnt = 0
for i in range(n):
	d = 0
	while 1:
		d += 1
		j = i + d
		k = j + d
		if not k < n:
			break
		if not (s[i] == s[j] or s[j] == s[k] or s[k] == s[i]):
			cnt += 1
print(r * g * b - cnt)


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)


N = int(input())
S = input()
x = S.count('R') * S.count('G') * S.count('B')
c = 0
for i in range(N):
	for j in range(i + 1, N):
		if S[i] != S[j]:
			k = 2 * j - i
			if k < N and S[k] != S[i] and (S[k] != S[j]):
				c += 1
print(x - c)


N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N - 2):
	r = S[i]
	for j in range(i + 1, N - 1):
		g = S[j]
		if r == g:
			continue
		k = 2 * j - i
		if k >= N:
			continue
		b = S[k]
		if r != b and g != b:
			ans -= 1
print(ans)


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


def abc162d_rgb_triplets():
	n = int(input())
	s = input()
	r_cnt = 0
	g_cnt = 0
	b_cnt = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'R':
			r_cnt += 1
		elif s[i] == 'G':
			g_cnt += 1
		else:
			b_cnt += 1
	result = r_cnt * g_cnt * b_cnt
	for i in range(n - 2):
		for j in range(i + 1, n - 1):
			k = j + (j - i)
			if s[i] == s[j] or k >= n or s[i] == s[k] or (s[j] == s[k]):
				continue
			result -= 1
	print(result)
abc162d_rgb_triplets()


import sys
from collections import Counter
N = int(input())
S = list(input())
Sc = Counter(S)
R = Sc['R']
G = Sc['G']
B = Sc['B']
if R == 0 or G == 0 or B == 0:
	print(0)
	return
else:
	ans = R * G * B
for i in range(1, N // 2 + 2):
	for j in range(N):
		if j + i + i >= N:
			break
		if S[j] == S[j + i]:
			continue
		if S[j] == S[j + i + i]:
			continue
		if S[j + i] == S[j + i + i]:
			continue
		ans -= 1
print(ans)


n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = n - r - g
total = r * g * b
for i in range(n):
	for j in range(i + 1, n):
		if 2 * j - i < n and len({s[i], s[j], s[2 * j - i]}) == 3:
			total -= 1
print(total)


N = int(input())
S = [1 if x == 'R' else 2 if x == 'G' else 3 for x in [*input()]]
t = S.count(1) * S.count(2) * S.count(3)
for i in range(N - 2):
	j = 1
	while i + 2 * j < N:
		if S[i] * S[i + j] * S[i + 2 * j] == 6:
			t -= 1
		j += 1
print(t)


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


from collections import Counter
import sys
input = sys.stdin.readline

def main():
	N = int(input())
	S = input()
	ans = 0
	(r, g, b) = ([0 for _ in range(N + 1)], [0 for _ in range(N + 1)], [0 for _ in range(N + 1)])
	for i in range(N):
		(r[i + 1], g[i + 1], b[i + 1]) = (r[i], g[i], 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(1, N - 1):
		if S[i] == 'R':
			ans += g[i] * (b[N] - b[i + 1]) + b[i] * (g[N] - g[i + 1])
			for j in range(1, min(i, N - i - 1) + 1):
				if {S[i - j], S[i + j]} == {'G', 'B'}:
					ans -= 1
		elif S[i] == 'G':
			ans += r[i] * (b[N] - b[i + 1]) + b[i] * (r[N] - r[i + 1])
			for j in range(1, min(i, N - i - 1) + 1):
				if {S[i - j], S[i + j]} == {'R', 'B'}:
					ans -= 1
		elif S[i] == 'B':
			ans += g[i] * (r[N] - r[i + 1]) + r[i] * (g[N] - g[i + 1])
			for j in range(1, min(i, N - i - 1) + 1):
				if {S[i - j], S[i + j]} == {'G', 'R'}:
					ans -= 1
	print(ans)
main()


n = int(input())
s = input()
c = s.count
(r, g, b) = (c('R'), c('G'), c('B'))
t = 0
for i in range(n - 2):
	for j in range(i + 1, n - 1):
		k = j * 2 - i
		if k < n:
			if s[i] + s[j] + s[k] in ('RGB', 'RBG', 'GRB', 'GBR', 'BRG', 'BGR'):
				t += 1
print(r * g * b - t)


N = int(input())
S = list(input())
A = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
	for j in range(i + 1, N):
		k = j - i + j
		if k < N and S[k] != S[j] and (S[k] != S[i]) and (S[i] != S[j]):
			A -= 1
print(A)


N = int(input())
S = input()
count = 0
r = S.count('R')
g = S.count('G')
b = S.count('B')
count += r * g * b
for i in range(0, N - 1):
	for j in range(i + 1, N):
		k = 2 * j - i
		if k >= N:
			break
		if S[i] != S[j] and S[j] != S[k] and (S[i] != S[k]):
			count -= 1
print(count)


import collections
import sys
import math
sys.setrecursionlimit(10 ** 7)

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST():
	return list(MAP())

def NIJIGEN(H):
	return [list(input()) for i in range(H)]
N = INT()
S = input()
if N < 3:
	print(0)
	return
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r * g * b
for i in range(N - 2):
	for j in range(i + 1, N - 1):
		k = j * 2 - i
		if k >= N:
			continue
		if S[j] != S[i] and S[k] != S[i] and (S[j] != S[k]):
			ans -= 1
print(ans)


N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
cnt = 0
k = 1
while 2 * k + 1 <= N:
	for i in range(N - 2 * k):
		if S[i] != S[i + k] and S[i + k] != S[i + 2 * k] and (S[i + 2 * k] != S[i]):
			cnt += 1
	k += 1
print(r * g * b - cnt)


from collections import *
N = int(input())
S = input()
c = Counter(S)
ans = c['R'] * c['G'] * c['B']
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
		ans -= 1
print(ans)


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


from collections import Counter as C
n = int(input())
S = list(input())
a = 1
L = C(S).most_common(3)
for l in L:
	a *= l[1]
for i in range(1, n):
	for j in range(n - 2 * i):
		if len(set((S[j], S[j + i], S[j + 2 * i]))) == 3:
			a -= 1
print(a if len(L) > 2 else 0)


def main():
	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 j - i > n - j - 1:
				break
			if s[i] != s[j] and s[j] != s[2 * j - i] and (s[2 * j - i] != s[i]):
				ans -= 1
	print(ans)

def __starting_point():
	main()
__starting_point()


n = int(input())
s = input()
p = s.count('R') * s.count('G') * s.count('B')
for j in range(1, n - 1):
	for i in range(1, min(j, n - j - 1) + 1):
		(x, y, z) = (s[j - i], s[j], s[j + i])
		if x != y and y != z and (z != x):
			p -= 1
print(p)


N = int(input())
S = input()
count = 0
count = S.count('R') * S.count('G') * S.count('B')
for i in range(N - 2):
	for j in range(1, (N + i + 1) // 2 - i):
		if S[i] + S[i + j] + S[i + 2 * j] in ['RGB', 'RBG', 'GRB', 'GBR', 'BGR', 'BRG']:
			count = count - 1
print(count)


from functools import reduce
from operator import mul
from collections import Counter

def solve():
	N = int(input())
	S = input()
	ret = 0
	RGB_ = {'R': 0, 'G': 0, 'B': 0}
	counter = Counter(S)
	for (k, v) in counter.items():
		RGB_[k] += v
	ret = reduce(mul, RGB_.values())
	RGB_mergin_len = 0
	while 3 + RGB_mergin_len * 2 <= N:
		for i in range(N - (3 + RGB_mergin_len * 2) + 1):
			j = i + 1 + RGB_mergin_len
			k = i + 2 + 2 * RGB_mergin_len
			if S[i] != S[j] and S[j] != S[k] and (S[i] != S[k]):
				ret -= 1
		RGB_mergin_len += 1
	print(ret)
solve()


n = int(input())
ss = list(input())
res = ss.count('R') * ss.count('G') * ss.count('B')
for i in range(n):
	for j in range(i + 1, n):
		k = 2 * j - i
		if k >= n:
			continue
		if all([ss[i] != ss[j], ss[j] != ss[k], ss[k] != ss[i]]):
			res -= 1
print(res)


n = int(input())
s = input()
(r, g, b) = ([], [], [])
cnt = 1
for i in s:
	if i == 'R':
		r.append(cnt)
	elif i == 'G':
		g.append(cnt)
	else:
		b.append(cnt)
	cnt += 1
rd = set(map(lambda x: x * 2, r))
gd = set(map(lambda x: x * 2, g))
bd = set(map(lambda x: x * 2, b))
ddc = 0
for i in r:
	for j in g:
		if i + j in bd:
			ddc += 1
for j in g:
	for k in b:
		if j + k in rd:
			ddc += 1
for i in r:
	for k in b:
		if i + k in gd:
			ddc += 1
print(len(r) * len(g) * len(b) - ddc)


n = int(input())
s = input()
from collections import Counter
c = Counter(s)
t = c['R'] * c['G'] * c['B']
for i in range(n):
	for j in range(i + 1, min(n, n + 1 + i >> 1)):
		k = 2 * j - i
		if k < n and s[i] != s[j] and (s[j] != s[k]) and (s[k] != s[i]):
			t -= 1
print(t)


input()
s = input()
(r, g, b) = map(s.count, 'RGB')
print(r * g * b - sum((1 for (i, c1) in enumerate(s) for (j, c2) in enumerate(s[i::2]) if c1 != c2 != s[i + j] != c1)))


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


N = int(input())
S = input()
(r, g, b) = (S.count('R'), S.count('G'), S.count('B'))
total = r * g * b
count = 0
for d in range(1, N):
	for i in range(N - 2 * d):
		if S[i] != S[i + d] and S[i + d] != S[i + 2 * d] and (S[i + 2 * d] != S[i]):
			count += 1
print(total - count)


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


import sys
from collections import Counter

def main():
	N = int(sys.stdin.readline())
	S = sys.stdin.readline().rstrip()
	cnt_dic = Counter(S)
	ans = cnt_dic['R'] * cnt_dic['G'] * cnt_dic['B']
	for i in range(N):
		for j in range(i + 1, N):
			k = 2 * j - i
			if k < len(S) and S[i] != S[j] and (S[j] != S[k]) and (S[k] != S[i]):
				ans -= 1
	print(ans)

def __starting_point():
	main()
__starting_point()


n = int(input())
s = input()
all = s.count('R') * s.count('G') * s.count('B')
for x in range(n - 1):
	for y in range(1, (n - 1 - x) // 2 + 1):
		if s[x] != s[x + y] and s[x] != s[x + 2 * y] and (s[x + y] != s[x + 2 * y]):
			all -= 1
print(all)


N = int(input())
List = list(input())
ans = List.count('R') * List.count('G') * List.count('B')
for i in range(1, 2000):
	for j in range(N - i * 2):
		if {List[j], List[j + i], List[j + 2 * i]} == {'R', 'G', 'B'}:
			ans -= 1
print(ans)


n = int(input())
s = input()
R = []
G = []
B = []

def f():
	for i in range(n):
		if s[i] == 'R':
			R.append(i)
		elif s[i] == 'G':
			G.append(i)
		else:
			B.append(i)
	r = len(R)
	g = len(G)
	b = len(B)
	if r == 0 or g == 0 or b == 0:
		return 0
	else:
		ans = r * g * b
		m = 0
		for j in range(1, n - 1):
			for k in range(1, min(j, n - 1 - j) + 1):
				lt = s[j - k]
				md = s[j]
				rt = s[j + k]
				if lt != md and md != rt and (rt != lt):
					m += 1
		return ans - m
print(f())


N = int(input())
S = list(input())
r = S.count('R')
g = S.count('G')
b = S.count('B')
cnt = 0
for i in range(N - 3):
	if S[i] == 'R':
		r -= 1
		cnt += g * b
		l = 1
		while i + 2 * l < N:
			if S[i + l] == 'G' and S[i + 2 * l] == 'B' or (S[i + l] == 'B' and S[i + 2 * l] == 'G'):
				cnt -= 1
			l += 1
	if S[i] == 'G':
		g -= 1
		cnt += r * b
		l = 1
		while i + 2 * l < N:
			if S[i + l] == 'R' and S[i + 2 * l] == 'B' or (S[i + l] == 'B' and S[i + 2 * l] == 'R'):
				cnt -= 1
			l += 1
	if S[i] == 'B':
		b -= 1
		cnt += g * r
		l = 1
		while i + 2 * l < N:
			if S[i + l] == 'G' and S[i + 2 * l] == 'R' or (S[i + l] == 'R' and S[i + 2 * l] == 'G'):
				cnt -= 1
			l += 1
print(cnt)


def main() -> None:
	n = int(input())
	s = input()
	answer = s.count('R') * s.count('G') * s.count('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[j] != s[k]) and (s[k] != s[i]):
				answer -= 1
	print(answer)
	return

def __starting_point():
	main()
__starting_point()


N = int(input())
S = input()
(cntR, cntG, cntB) = (S.count('R'), S.count('G'), S.count('B'))
ans = cntR * cntG * cntB
for i in range(N - 2):
	for j in range(i + 1, N - 1):
		if S[i] != S[j]:
			k = 2 * j - i
			if k < N and S[k] != S[i] and (S[k] != S[j]):
				ans -= 1
print(ans)


n = int(input())
s = input()
c = s.count
print(c('R') * c('G') * c('B') - sum((''.join(sorted(s[i:i + w * 2 + 1:w])) == 'BGR' for i in range(n - 2) for w in range(1, (n - i + 1) // 2))))


n = int(input())
s = str(input())
(r, g, b) = map(s.count, 'RGB')
ans = r * g * b
for i in range(n):
	for j in range(i + 1, n):
		k = 2 * j - i
		if k < n and len(set([s[i], s[j], s[k]])) == 3:
			ans -= 1
print(ans)


from collections import Counter
n = int(input())
s = input()
c = Counter(s)
ans = c['R'] * c['G'] * c['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] != s[k] != s[i]:
			ans -= 1
print(ans)


N = int(input())
S = list(input())
ans = S.count('R') * S.count('G') * S.count('B')
i = 0
while i < N - 1:
	j = i + 1
	while 2 * j - i < N:
		ans -= 1 * (S[i] != S[j]) * (S[j] != S[2 * j - i]) * (S[2 * j - i] != S[i])
		j += 1
	i += 1
print(ans)


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


import sys
from collections import deque, Counter, defaultdict
import bisect
import heapq
import math
import itertools
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
inf = 10 ** 18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
mod = 998244353
n = ri()
s = rs()
(r_li, g_li, b_li) = ([], [], [])
for i in s:
	if i == 'R':
		r_li.append(i)
	if i == 'G':
		g_li.append(i)
	if i == 'B':
		b_li.append(i)
ans = len(r_li) * len(g_li) * len(b_li)
cnt = 0
for res in range(1, n // 2 + 1):
	for i in range(0, n):
		if i + 2 * res <= n - 1:
			if s[i] != s[i + res] and s[i + res] != s[i + res + res] and (s[i + res + res] != s[i]):
				cnt += 1
		else:
			break
print(ans - cnt)


n = int(input())
(s, rem) = (input(), 0)
ans = s.count('R') * s.count('B') * s.count('G')
for i in range(1, (n - 1) // 2 + 1):
	for j in range(i * 2, n):
		if s[j] + s[j - i] + s[j - 2 * i] in ('RGB', 'RBG', 'GRB', 'GBR', 'BGR', 'BRG'):
			rem += 1
print(ans - rem)


n = int(input())
s = input()
total = 1
for c in 'RGB':
	total *= s.count(c)
for i in range(1, n - 1):
	for j in range(1, min(i + 1, n - i)):
		if s[i] != s[i - j] and s[i - j] != s[i + j] and (s[i] != s[i + j]):
			total -= 1
print(total)


n = int(input())
s = input()
from collections import Counter
c = Counter(s)
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):
		k = j + (j - i)
		if k >= n:
			break
		elif s[i] != s[j] and s[j] != s[k] and (s[k] != s[i]):
			cnt += 1
print(r * g * b - cnt)


def main():
	N = int(input())
	S = input()
	cnt = 0
	for i in range(N):
		for j in range(i + 1, N):
			k = 2 * j - i
			if k >= N:
				continue
			if S[j] != S[i] and S[i] != S[k] and (S[k] != S[j]):
				cnt += 1
	print(S.count('R') * S.count('B') * S.count('G') - cnt)

def __starting_point():
	main()
__starting_point()


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)


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


from collections import Counter
n = int(input())
s = input()
c = Counter(s)
ans = c['R'] * c['G'] * c['B']
lim = n // 2
for i in range(1, n // 2 + 1):
	for j in range(i, n - i):
		l = s[j - i]
		m = s[j]
		r = s[j + i]
		if l != m and m != r and (r != l):
			ans -= 1
print(ans)


n = int(input())
s = list(input())
cnt = 0
for j in range(2, n, 2):
	for i in range(0, n - j):
		k = (2 * i + j) // 2
		if s[i] != s[i + j] and s[i] != s[k] and (s[k] != s[i + j]):
			cnt += 1
(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
print(r * g * b - cnt)


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


N = int(input())
S = input()
all = S.count('R') * S.count('G') * S.count('B')
for i in range(N - 1):
	for j in range(i, N):
		if 0 <= 2 * i - j < N:
			if S[2 * i - j] != S[i] != S[j] != S[2 * i - j]:
				all -= 1
print(all)


n = int(input())
s = input()
c = s.count
m = c('R') * c('G') * c('B')
for i in range(n):
	for j in range(i + 2, n, 2):
		if s[i] != s[i + j >> 1] != s[j] != s[i]:
			m -= 1
print(m)


def main():
	import sys
	pin = sys.stdin.readline
	pout = sys.stdout.write
	perr = sys.stderr.write
	N = int(pin())
	S = pin()[:-1]
	r = 0
	g = 0
	b = 0
	cnt = 0
	for i in range(N):
		if S[i] == 'R':
			r += 1
		elif S[i] == 'G':
			g += 1
		elif S[i] == 'B':
			b += 1
		for j in range(i + 1, N):
			k = 2 * j - i
			if k >= N:
				continue
			if S[i] != S[j] and S[j] != S[k] and (S[k] != S[i]):
				cnt += 1
	print(r * g * b - cnt)
	return
main()


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


from functools import reduce
from operator import mul

def solve():
	N = int(input())
	S = input()
	RGB_count = {'R': 0, 'G': 0, 'B': 0}
	RGB_set = {'R', 'G', 'B'}
	ret = 0
	for s in S:
		ret += reduce(mul, [RGB_count[t] for t in RGB_set - {s}])
		RGB_count[s] = RGB_count[s] + 1
	RGB_mergin_len = 0
	while 3 + RGB_mergin_len * 2 <= N:
		for i in range(N - (3 + RGB_mergin_len * 2) + 1):
			j = i + 1 + RGB_mergin_len
			k = i + 2 + 2 * RGB_mergin_len
			if S[i] != S[j] and S[j] != S[k] and (S[i] != S[k]):
				ret -= 1
		RGB_mergin_len += 1
	print(ret)
solve()


n = int(input())
S = input().strip()
(r, g, b) = map(S.count, 'RGB')
t = 0
for i in range(n - 2):
	for w in range(1, (n - i + 1) // 2):
		t += len({S[i], S[i + w], S[i + 2 * w]}) == 3
print(r * g * b - t)


from sys import stdin
nii = lambda : map(int, stdin.readline().split())
lnii = lambda : list(map(int, stdin.readline().split()))
from collections import Counter
n = int(input())
s = input()
c = Counter(s)
ans = c['R'] * c['G'] * c['B']
lim = n // 2
for i in range(1, n // 2 + 1):
	for j in range(i, n - i):
		l = s[j - i]
		m = s[j]
		r = s[j + i]
		if l != m and m != r and (r != l):
			ans -= 1
print(ans)


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


n = int(input())
s = input()
ans = s.count('R') * s.count('G') * s.count('B')
for j in range(n):
	J = s[j]
	for i in range(j):
		k = 2 * j - i
		if k < n:
			I = s[i]
			K = s[k]
			if I != J != K != I:
				ans -= 1
print(ans)


n = int(input())
s = input()
(r, g, b) = map(s.count, 'RGB')
print(r * g * b - len([1 for i in range(n) for j in range(i, n, 2) if s[i] != s[j] != s[i + j >> 1] != s[i]]))


N = int(input())
S = list(input())
(r, g, b) = (0, 0, 0)
for s in S:
	if s == 'R':
		r += 1
	elif s == '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 = 2 * j - i
		if k < N:
			if S[i] != S[j] and S[j] != S[k] and (S[k] != S[i]):
				ans -= 1
print(ans)


N = int(input())
S = input()
(r, g, b) = ([], [], [])
for (idx, w) in enumerate(S):
	if w == 'R':
		r.append(idx + 1)
	elif w == 'G':
		g.append(idx + 1)
	elif w == 'B':
		b.append(idx + 1)
if len(r) >= len(b) and len(r) >= len(g):
	(L, M, S) = (r, g, b)
elif len(b) >= len(r) and len(b) >= len(g):
	(L, M, S) = (b, g, r)
elif len(g) >= len(r) and len(g) >= len(b):
	(L, M, S) = (g, r, b)
L = set(L)
ans = len(S) * len(L) * len(M)
for s in S:
	for m in M:
		tmp = sorted([s, m])
		if tmp[0] - (tmp[1] - tmp[0]) in L:
			ans -= 1
		if tmp[1] + (tmp[1] - tmp[0]) in L:
			ans -= 1
		if sum(tmp) / 2 in L:
			ans -= 1
print(ans)


def main():
	N = int(input())
	S = input()
	dict = {'R': 0, 'B': 0, 'G': 0}
	for i in range(N):
		dict[S[i]] += 1
	ans = dict['R'] * dict['B'] * dict['G']
	for i in range(N - 2):
		if (N - i) % 2 == 0:
			tmp = int((N - i) / 2) - 1
		else:
			tmp = (N - i) // 2
		for j in range(1, tmp + 1):
			if S[i] != S[i + j] and S[i] != S[i + 2 * j] and (S[i + j] != S[i + 2 * j]):
				ans = ans - 1
	return ans
print(main())


N = int(input())
S = 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
ms = (N - 3) // 2
t = 0
for i in range(ms + 1):
	for j in range(N - (2 * i + 3) + 1):
		if S[j] != S[j + i + 1] and S[j] != S[j + 2 * i + 2] and (S[j + i + 1] != S[j + 2 * i + 2]):
			t += 1
print(r * g * b - t)