Problem:
 Score : 400 points    Problem Statement We have a string S of length N consisting of R , G , and B .  Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:   S_i \neq S_j , S_i \neq S_k , and S_j \neq S_k .  j - i \neq k - j .       Constraints  1 \leq N \leq 4000  S is a string of length N consisting of R , G , and B .         Input Input is given from Standard Input in the following format:  N  S       Output Print the number of triplets in question.        Sample Input 1 4
RRGB      Sample Output 1 1  Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.       Sample Input 2 39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB      Sample Output 2 1800

Solution (Python):
from collections import Counter

N = int(input())
S = input()
RGB_Counter = Counter(S)
ans = RGB_Counter['R'] * RGB_Counter['G'] * RGB_Counter['B']

dum = []
d = 0
while 3 + d*2 <= N:
    for i in range(N - 2*(d+1)):
        a = S[i]
        b = S[i+d+1]
        c = S[i+2*(d+1)]
        if a != b and b != c and c != a:
            ans -= 1
    d += 1

print(ans)

Solution (Python):
n = int(input())
s = input()
 
r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')
 
ans = r_cnt * g_cnt * b_cnt
 
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)

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

print(ans)

Solution (Python):
n = int(input())
s = input()
rcnt = s.count('R')
gcnt = s.count('G')
bcnt = s.count('B')

ans = rcnt*gcnt*bcnt

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)

Solution (Python):
from collections import defaultdict
n = int(input())
S = input()
ans = S.count("R") * S.count("G") * S.count("B")
for i in range(n):
    for r in range(1,n):
        if i + r < n and 0 <= i - r:
            if S[i + r] != S[i - r] and S[i] != S[i - r]and S[i] != S[i + r]:
                ans -=1

print(ans)

Solution (Python):
n = int(input())
s = list(input())
tot = 0
tot += 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 k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            tot -= 1
print(tot)

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

cnt = s.count("R") * s.count("G") * s.count("B")

for i in range(n - 1):
    tmp = s[i]
    for j in range(i + 1, (n + i + 1) // 2):
        if s[2 * j - i] != s[j] and s[j] != tmp and s[2 * j - i] != tmp:
            cnt -= 1
print(cnt)

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

print(ans)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
s = list(input())
baf = s.count('B')
raf = s.count('R')
gaf = s.count('G')
bbf = 0
rbf = 0
gbf = 0
ans = 0
for i,item in enumerate(s):
    if item == 'B':
        baf -= 1
        bbf += 1
        ans += rbf*gaf
        ans += raf*gbf
    elif item == 'R':
        raf -= 1
        rbf += 1
        ans += bbf*gaf
        ans += baf*gbf
    else:
        gaf -= 1
        gbf += 1
        ans += rbf*baf
        ans += raf*bbf
cnt = 0
for i in range(n):
    num = min(i,n-1-i)
    for j in range(1,num+1):
        if s[i-j] != s[i+j] and s[i-j] != s[i] and s[i+j] != s[i]:
            cnt += 1
print(ans-cnt)

Solution (Python):
n = int(input())
s = input()
 
r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')
 
ans = r_cnt * g_cnt * b_cnt
 
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)

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(i+1,N):
        k=2*j-i
        if k<N and S[k]!=S[i] and S[k]!=S[j] and S[j]!=S[i]:
            ans-=1

print(ans)

Solution (Python):
n = int(input())
s = input()
if n <= 3:
    sum = 0
else:
    sum = 0
    r = []
    g = []
    b = []
    if s[n-1] == "R":
        r.append(1)
        g.append(0)
        b.append(0)
    if s[n-1] == "G":
        r.append(0)
        g.append(1)
        b.append(0)
    if s[n-1] == "B":
        r.append(0)
        g.append(0)
        b.append(1)
    for i in range(1,n):
        if s[n-i-1] == "R":
            r.append(r[i-1]+1)
            g.append(g[i-1])
            b.append(b[i-1])
        if s[n-i-1] == "G":
            r.append(r[i-1])
            g.append(g[i-1]+1)
            b.append(b[i-1])
        if s[n-i-1] == "B":
            r.append(r[i-1])
            g.append(g[i-1])
            b.append(b[i-1]+1)
    for i in range(n-3):
        if s[i] == "R":
            j = i + 1
            while j <= n-2:
                if s[j] == "G":
                    sum += b[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "B":
                        sum -= 1
                if s[j] == "B":
                    sum += g[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "G":
                        sum -= 1
                j += 1
        if s[i] == "G":
            j = i + 1
            while j <= n-2:
                if s[j] == "R":
                    sum += b[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "B":
                        sum -= 1
                if s[j] == "B":
                    sum += r[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "R":
                        sum -= 1
                j += 1
        if s[i] == "B":
            j = i + 1
            while j <= n-2:
                if s[j] == "R":
                    sum += g[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "G":
                        sum -= 1
                if s[j] == "G":
                    sum += r[n-j-1]
                    if 2*j-i <= n-1 and s[2*j-i] == "R":
                        sum -= 1
                j += 1
print(sum)

Solution (Python):
n=int(input())
s=input()
c=s.count
a=c("R")*c("G")*c("B")
for i in range(n-2):
    for j in range((n-i+1)//2):
        if s[i]!=s[i+j] and s[i+j]!=s[i+2*j] and s[i+2*j]!=s[i]:
            a-=1
print(a)

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)
lr, lg, lb = len(r), len(g), len(b)
ans = lr * lg * lb

for i in r:
    for j in g:
        a = 2 * j - i
        c = 2 * i - j
        if 0 <= a and a <= n-1 and s[a] == 'B':
            ans -= 1
        if 0 <= c and c <= n-1 and s[c] == 'B':
            ans -= 1
        if (i + j) % 2 == 0 and s[(i + j) // 2] == 'B':
            ans -= 1
print(ans)

Solution (Python):
from collections import Counter
 
n = int(input())
s = input()
try:
  x, y, z = Counter(s).values()
  print(x * y * z - sum(s[i] != s[(i + j) // 2] != s[j] != s[i]
                        for i in range(n) for j in range(i + 2, n, 2)))
except ValueError:
  print(0)

Solution (Python):
import bisect
 
n = int(input())
s = input()
 
RGB = [[], [], []]
for i in range(n):
    if s[i] == 'R':
        RGB[0].append(i)
    elif s[i] == 'G':
        RGB[1].append(i)
    else:
        RGB[2].append(i)
total = len(RGB[0]) * len(RGB[1]) * len(RGB[2])
len_B=len(RGB[2])
for r in RGB[0]:
    for g in RGB[1]:
        dif = abs(r - g)
        up = max(r,g)+dif
        down = min(r,g)-dif
        up_posi=bisect.bisect_left(RGB[2],up)
 
        if up_posi < len_B:
            if RGB[2][up_posi]==up:
                total-=1
        down_posi=bisect.bisect_left(RGB[2],down)
        if down_posi < len_B:
            if RGB[2][down_posi]==down:
                total-=1
        if dif % 2 == 0:
            center = (r+g)/2
            center_posi=bisect.bisect_left(RGB[2], center)
            if center_posi < len_B:
                if RGB[2][center_posi]==center:
                    total-=1
print(total)

Solution (Python):
def main():
    n = int(input())
    s = input()
    
    r = set()
    g = set()
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = set(range(n)) - r - g

    ans = 0
    for i in r:
        for j in g:
            p, q = min(i, j), max(i, j)
            if (q - p) % 2 == 0 and (p + q) // 2 in b:
                ans -= 1
            if (q + (q - p)) in b:
                ans -= 1
            if (p - (q - p)) in b:
                ans -= 1
            ans += len(b)
    return ans


if __name__ == '__main__':
    print(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(N-2):
    for j in range(i,N-1):
        k = 2*j - i
        if k < N:
            tmp = []
            tmp.append(S[i])
            tmp.append(S[j])
            tmp.append(S[k])
            if len(set(tmp)) == 3:
                ans -= 1
 
print(ans)

Solution (Python):
from collections import Counter

input()
s = input()
n = len(s)
try:
  x, y, z = Counter(s).values()
  print(x * y * z - sum(s[i] != s[(i + j) // 2] != s[j] != s[i]
                        for i in range(n) for j in range(i + 2, n, 2)))
except ValueError:
  print(0)

Solution (Python):
N = int(input())
s = input()
R_cnt = [0] * (N + 1)
B_cnt = [0] * (N + 1)
G_cnt = [0] * (N + 1)
for i in range(N):
    if s[i] == 'R':
        R_cnt[i + 1] = R_cnt[i] + 1
        B_cnt[i + 1] = B_cnt[i]
        G_cnt[i + 1] = G_cnt[i]
    elif s[i] == 'B':
        R_cnt[i + 1] = R_cnt[i]
        B_cnt[i + 1] = B_cnt[i] + 1
        G_cnt[i + 1] = G_cnt[i]
    else:
        R_cnt[i + 1] = R_cnt[i]
        B_cnt[i + 1] = B_cnt[i]
        G_cnt[i + 1] = G_cnt[i] + 1
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if s[i] == s[j]:
            continue
            
        if s[i] == 'R' and s[j] == 'B' or s[i] == 'B' and s[j] == 'R':
            ans += G_cnt[N] - G_cnt[j]
        elif s[i] == 'B' and s[j] == 'G' or s[i] == 'G' and s[j] == 'B':
            ans += R_cnt[N] - R_cnt[j]
        elif s[i] == 'R' and s[j] == 'G' or s[i] == 'G' and s[j] == 'R':
            ans += B_cnt[N] - B_cnt[j]
        if j + (j - i) < N and s[i] != s[j] != s[j + (j - i)] != s[i]:
            ans -= 1
print(ans)

Solution (Python):
import bisect
import itertools
 
n = int(input())
s = input()
 
RGB = [[], [], []]
for i in range(n):
    if s[i] == 'R':
        RGB[0].append(i)
    elif s[i] == 'G':
        RGB[1].append(i)
    else:
        RGB[2].append(i)
total = len(RGB[0]) * len(RGB[1]) * len(RGB[2])
RGB.sort(key=lambda x:len(x))
len_B=len(RGB[2])
max_B=max(RGB[2])
min_B=min(RGB[2])


for rg in itertools.product(RGB[0],RGB[1]):
    r=rg[0]
    g=rg[1]
    dif = abs(r - g)
 
    up = max(r,g)+dif
    down = min(r,g)-dif
    if max_B >=up:
        if RGB[2][bisect.bisect_left(RGB[2],up)]==up:
            total-=1
    if min_B <= down and max_B >= down:
        if RGB[2][bisect.bisect_left(RGB[2],down)]==down:
            total-=1
    if dif % 2 == 0:
        center = (r+g)/2
        center_posi=bisect.bisect_left(RGB[2], center)
        if center_posi < len_B:
            if RGB[2][center_posi]==center:
                total-=1
print(total)

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

r = g = 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
ans = r * g * b

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

Solution (Python):


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

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

counter = r_cnt * g_cnt * b_cnt

for i in range(n):
    for p in range(n):
        j = i + p
        k = j + p
        if k >= n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            counter -= 1
print(counter)

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

Solution (Python):
def main():
    n = int(input())
    s = input()
    
    r = set()
    g = set()
    r_add = r.add
    g_add = g.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
    b = set(range(n)) - r - g

    ans = 0
    for i in r:
        for j in g:
            p, q = min(i, j), max(i, j)
            if (q - p) % 2 == 0 and (p + q) // 2 in b:
                ans -= 1
            if (q + (q - p)) in b:
                ans -= 1
            if (p - (q - p)) in b:
                ans -= 1
            ans += len(b)
    return ans


if __name__ == '__main__':
    print(main())

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

r = g = 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
ans = r * g * b

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

Solution (Python):



n = int(input())
s = input()
flag = True
if len(set(s))<=2:
    print(0)
    flag = False
if flag:
    r = [i for i in range(n) if s[i]=='R']
    b = [i for i in range(n) if s[i]=='B']
    g = [i for i in range(n) if s[i]=='G']
    minus = 0
    def cnt_cmb(a, b, c):
        cnt = 0
        set_a = set(a)
        set_b = set(b)
        set_c = set(c)
        for i in set_a:
            for j in set_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)
    print(len(r)*len(b)*len(g)-minus)

Solution (Python):
n = int(input())
s = input()
L = {"R": 0, "G": 0, "B": 0}
R = {"R": s.count("R"), "G": s.count("G"), "B": s.count("B")}
ans = 0
L[s[0]]+=1
R[s[0]]-=1
for i in range(1, n - 1):
	t = s[i]
	if t == "R":
		ans += L["G"] * R["B"] + L["B"] * R["G"]
	elif t == "G":
		ans += L["R"] * R["B"] + L["B"] * R["R"]
	else:
		ans += L["R"] * R["G"] + L["G"] * R["R"]
	for j in range(min(i,n-1-i)):
		ans -= t!=s[i-j-1]!=s[i+j+1]!=t
	L[t]+=1
	R[t]-=1
print(ans)

Solution (Python):
n = int(input())
s = input()
c = [0, 0, 0]
col1 = []
col2 = []
col3 = []
res = 0

for i in range(n):
    if s[i] == "R":
        col1.append(i)
    elif s[i] == "G":
        col2.append(i)
    else:
        col3.append(i)

res = len(col1) * len(col2) * len(col3)

for i in range((n + 1) // 2):
    for j in range(n - (i + 1) * 2):
        x = s[j]
        y = s[j + i + 1]
        z = s[j + (i + 1) * 2]
        if x == y or y == z or x == z:
            continue
        else:
            res = res - 1

print(res)

Solution (Python):
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

n = int(input())
s = input()
d = defaultdict(list)
has = defaultdict(set)
for i, c in enumerate(s):
    d[c].append(i)
    has[c].add(i)
ret = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        need = list({'R', 'G', 'B'} - {s[i], s[j]})[0]
        a = d[need]
        left, right = -1, len(a)
        while left + 1 < right:
            mid = (left + right) >> 1
            if a[mid] <= j:
                left = mid
            else:
                right = mid
        if right != len(a):
            bad = j + j - i
            ret += len(a) - right - (bad in has[need])
print(ret)

Solution (Python):
import collections

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

dic = collections.Counter(S)


cnt = 1
c = 0
for k, v in dic.items():
    c += 1
    cnt *= v

if v == 0 or c != 3:
    print(0)
else:
    for j in range(1, N - 1):
        mm = min(j, N - j - 1)
        for d in range(1, mm + 1):
            if S[j - d] != S[j] and S[j] != S[j + d] and S[j - d] != S[j + d]:
                cnt -= 1

    print(cnt)

Solution (Python):
from collections import Counter

input()
s = input()
try:
  x, y, z = Counter(s).values()
  print(x * y * z - sum(s[i] != s[(i + j) // 2] != s[j] != s[i]
                        for i in range(len(s)) for j in range(i % 2, i, 2)))
except ValueError:
  print(0)

Solution (Python):
from collections import Counter

N = int(input())
S = input()
index = Counter(S)
ans = index["R"]*index["G"]*index["B"]

for diff in range(1, N//2 +1):
    for i in range(N):
        j = i +diff
        k = j +diff
        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()

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

print(S.count("R")*S.count("G")*S.count("B")-cnt)

Solution (Python):


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n = int(input())
    s = input().rstrip()
    res = s.count('R') * s.count('G') * s.count('B')

    cnt = 0
    for j in range(1, n-1):
        for k in range(1, j+1):
            if j+k >= n:
                continue
            if s[j-k] != s[j] and s[j+k] != s[j] and s[j-k] != s[j+k]:
                cnt += 1

    print(res - cnt)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N=int(input())
    S=input()
    ans=S.count('R')*S.count('G')*S.count('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[j]!=S[k] and S[k]!=S[i]:
                ans-=1
    print(ans)
main()

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

r_list = []
g_list = []
for _ in range(n):
    if s[_] == "R":
        r_list.append(_)
    elif s[_] == "G":
        g_list.append(_)

count = 0
for r in r_list:
    for g in g_list:
        if min(r, g) - abs(r - g) > 0:
            if s[min(r, g) - abs(r - g)] == "B":
                count += 1
        if max(r, g) + abs(r - g) < n:
            if s[max(r, g) + abs(r - g)] == "B":
                count += 1
        if (r + g) % 2 == 0:
            if s[(r + g) // 2] == "B":
                count += 1


print(len(r_list) * len(g_list) * (n - len(r_list) - len(g_list)) - count)

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

cnt = 0
for i in range(n):
    for j in range(1, (n - 1) // 2 + 1):
        if n - 1 < i + 2 * j:
            break
        
        if s[i] != s[i + j] and s[i + j] != s[i + 2 * j] and s[i + 2 * j] != s[i]:
            cnt += 1
            continue
        
ans = [0] * 3
for word in s:
    if word == 'R':
        ans[0] += 1
    elif word == 'G':
        ans[1] += 1
    else:
        ans[2] += 1

ans_sum = ans[0] * ans[1] * ans[2]
print(ans_sum - cnt)

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

ans = 0
sn = len(s)

r = []
if s[0] == "R":
    r.append(1)
else:
    r.append(0)
g = []
if s[0] == "G":
    g.append(1)
else:
    g.append(0)
b = []
if s[0] == "B":
    b.append(1)
else:
    b.append(0)

for i in range(1, sn):
    if s[i] == "R":
        r.append(r[-1] + 1)
        g.append(g[- 1])
        b.append(b[- 1])
    elif s[i] == "G":
        r.append(r[-1])
        g.append(g[-1] + 1)
        b.append(b[- 1])
    elif s[i] == "B":
        r.append(r[-1])
        g.append(g[-1])
        b.append(b[-1] + 1)
    else:
        r.append(r[-1])
        g.append(g[- 1])
        b.append(b[- 1])

for i in range(n - 2):
    for j in range(i + 1, n - 1):
        if s[i] != s[j]:
            tmp = dict()
            tmp[s[i]] = 0
            tmp[s[j]] = 0
            target_idx = j + (j - i)

            if tmp.get("B", 1) == 1:
                ans += b[-1] - b[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "B":
                    ans -= 1
            elif tmp.get("G", 1) == 1:
                ans += g[-1] - g[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "G":
                    ans -= 1
            else:
                ans += r[-1] - r[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "R":
                    ans -= 1

print(ans)

Solution (Python):
import collections

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

c = collections.Counter(S)

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

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)

Solution (Python):
from itertools import product as pro

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

r = [i for i in range(n) if s[i] == 'R']
g = [i for i in range(n) if s[i] == 'G']
b = [i for i in range(n) if s[i] == 'B']
cnt = len(r) * len(g) * len(b)

max1=set(b)

for i, j in pro(r, g):
    if 2*j-i in max1:
        cnt -= 1
    if 2*i-j in max1:
        cnt -= 1
    if (i+j)/2 in max1:
        cnt -= 1

print(cnt)

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

Solution (Python):
N = int(input())
word = list(input())
ans = 0
for i in range(1,(N-1)//2+1):
    for j in range(N-2*i):
        if word[j] != word[j+i] and word[j] != word[j+2*i] and word[j+i] != word[j+2*i]:
            ans -= 1

RGB = {"R":0, "G":0, "B":0}
for i in range(N):
    RGB[word[i]] += 1
ans += RGB["R"]*RGB["G"]*RGB["B"]
print(ans)

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







N = int(input())

S = list(input())






R = []
G = []
B = []
for i in range(N):
    a = S[i]
    if a == "R":
        R.append(i)
    elif a == "G":
        G.append(i)
    else:
        B.append(i)
s = len(R) * len(G) * len(B)
def binary_search(S,key):  
    n = len(S)             
    left  = 0
    right = n
    while left < right:
        mid = (left+right) // 2
        if S[mid] == key:
            return mid
        elif S[mid] > key:
            right = mid
        else:
            left = mid + 1
    return -1
for r in R:
    for g in G:
        Min = min(r,g)
        Max = max(r,g)
        if 2 * Min - Max > 0:
            c1 = 2 * Min - Max
        else:
            c1 = -1
        if (Min + Max ) % 2 == 0:
            c2 = (Min + Max) / 2
        else:
            c2 = -1
        if 2 * Max - Min < N:
            c3 = 2 * Max - Min
        else:
            c3 = -1
        if c1 != -1:
            if binary_search(B,c1) != -1:
                s -= 1
        if c2 != -1:
            if binary_search(B,c2) != -1:
                s -= 1
        if c3 != -1:
            if binary_search(B,c3) != -1:
                s -= 1
print(s)

Solution (Python):
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
ret = 0
for i in range(1,n-1):
    m = min(i,n-i-1)
    for j in range(1,m+1):
        A,B,C = s[i],s[i-j],s[i+j]
        if A != B and A != C and B != C:
            ret += 1
ans = r*g*b - ret
print(ans)

Solution (Python):



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)
    print(len(r)*len(b)*len(g)-minus)

Solution (Python):
N = int(input())
S = input()
 
def im(l, x):
    return [i for i, _x in enumerate(l) if _x == x]
R = im(S, "R")
G = im(S, "G")
B = im(S, "B")
 
ext = 0
if N >= 3:
    for i in range((N-3)//2+1):
        for j in range(N-(3+2*i)+1):
            if S[j] != S[j+i+1] and S[j] != S[j+2*(i+1)] and S[j+i+1] != S[j+2*(i+1)]:
                ext += 1
 
ans = len(R)*len(G)*len(B) - ext
print(ans)

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

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

se=set(dic["B"])

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

Solution (Python):
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(len(r)*len(b)*len(g)-minus)

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

count = 0
r = []
g = []
b = []

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

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

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

Solution (Python):
from collections import Counter

def main():
    n = int(input())
    s = list(input())

    t = Counter(s)

    ans = t['R']*t['G']*t['B']

    for i in range(n):
        for j in range(n):
            if i+j*2<n:
                if s[i]!=s[i+j] and s[i+j]!=s[i+j*2] and s[i]!=s[i+j*2]:
                    ans-=1
            else:
                break
    print(ans)
if __name__ == "__main__":
    main()

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

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

lenr = len(r)
leng = len(g)
lenb = len(b)
total = lenr*leng*lenb
for i in range(0, n-2):
    for j in range(n-(i+1)*2):
        j1 = j+i+1
        j2 = j+i*2+2
        if (S[j] != S[j1]) and (S[j1] != S[j2]) and (S[j2] != S[j]):
            total -= 1
print(total)

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

Solution (Python):
def main():
    n=int(input())
    s=input()
    r=[]
    g=[]
    b=[]
    rdict={}
    gdict={}
    bdict={}
    ans=0
    for i in range(n):
        if s[i]=='R':
            r.append(i)
            rdict[2*i]=2*i
        if s[i]=='G':
            g.append(i)
            gdict[2*i]=2*i
        if s[i]=='B':
            b.append(i)
            bdict[2*i]=2*i
    ans=len(r)*len(g)*len(b)
    for i in r:
        for j in g:
            if i+j in bdict:
                ans-=1
    for i in g:
        for j in b:
            if i+j in rdict:
                ans-=1
    for i in b:
        for j in r:
            if i+j in gdict:
                ans-=1
    print(ans)
    
if __name__ == '__main__':
    main()