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):
n= int(input())
s = str(input())

r=set()
g=set()
b=set()
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
score = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        dist=abs(j-i)
        if (i>j):
            bi = i+dist
            if bi in b:
                score -=1
            bi = j-dist
            if bi in b:
                score -=1
        else:
            bi = j+dist
            if bi in b:
                score-=1
            bi = i-dist
            if bi in b:
                score-=1
        if((i+j)%2==0 and (i+j)/2 in b):
            score-=1

print(score)

Solution (Python):
from sys import stdin,stdout


def solve(S,N):
    color = [[] for _ in range(3)]

    for i in range(N):
        if S[i] == 'R': color[0].append(i)
        elif S[i] == 'G': color[1].append(i)
        else: color[2].append(i)
    
    
    
    total = len(color[0])*len(color[1])*len(color[2])

    remain = 0
    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]:
                remain += 1
    return total - remain


    


if __name__ == "__main__": 
    
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    stdout.write(str(solve(S,N)))

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)

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

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

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]


def main():
    N = int(input())
    S = list(input())
 
    c = [[] for _ in range(3)] 
    for i in range(len(S)):
        if S[i] == 'R':
            c[0].append(i)
        elif S[i] == 'G':
            c[1].append(i)
        else : 
            c[2].append(i)
    total = len(c[0]) * len(c[1]) * len(c[2])

    reigai = 0 
    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]:
                reigai += 1
    print(total - reigai)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]


def main():
    N = int(input())
    S = list(input())
 
    c = [[] for _ in range(3)] 
    for i in range(len(S)):
        if S[i] == 'R':
            c[0].append(i)
        elif S[i] == 'G':
            c[1].append(i)
        else : 
            c[2].append(i)
    total = len(c[0]) * len(c[1]) * len(c[2])

    reigai = 0 
    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]:
                reigai += 1
    print(total - reigai)


if __name__ == "__main__":
    main()

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

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

def main():

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

main()

Solution (Python):
N=int(input())
S=input()
r=S.count('R')
b=S.count('B')
g=S.count('G')
ans=r*b*g
for i in range(N):
    for s in range(N):
        j=i+s
        k=j+s
        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=str(input())

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

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

for ri in r:
    for gi in g:
        dist = abs(ri-gi)
        if ri > gi:
            bi = dist + ri
            if bi in b:
                d -= 1
            bi = gi - dist
            if bi in b:
                d -= 1
        else:
            bi = dist + gi
            if bi in b:
                d -= 1
            bi = ri - dist
            if bi in b:
                d -= 1

        if (ri + gi) % 2 == 0:
            bi = (ri+gi)/2
            if bi in b:
                d -= 1
print(d)

Solution (Python):
n=int(input())
s=str(input())
t=len(s)
r=set()
g=set()
b=set()
a=0
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
d=len(r)*len(g)*len(b)
for ri in r:
    for gi in g:
        dist = abs(ri-gi)
        if ri > gi:
            bi = dist + ri
            if bi in b:
                d -= 1
            bi = gi - dist
            if bi in b:
                d -= 1
        else:
            bi = dist + gi
            if bi in b:
                d -= 1
            bi = ri - dist
            if bi in b:
                d -= 1

        if (ri + gi) % 2 == 0:
            bi = (ri+gi)/2
            if bi in b:
                d -= 1
print(d)

Solution (Python):
n = int(input())
s = list(input())
ans = s.count('R') * s.count('G') * s.count('B')

for i in range(n - 1):
    for j in range(i+1, n):
        if 0 <= (2 * j - i) < n:
            if s[i] != s[j] and s[i] != s[2 * j - i] and s[2 * j - i] != s[j]:
                ans -= 1

print(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
    else:
        b += 1
c = r*g*b
for i in range(N - 2):
    for j in range(i, N - 1):
        k = 2 * j - i
        if k >= N:
            continue
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            c -= 1
print(c)

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

X=set()
Y=set()
Z=set()
for i in range(N):
    if S[i]=="R":
        X.add(i)
    elif S[i]=="G":
        Y.add(i)
    else:
        Z.add(i)

E=0
for a in X:
    for b in Y:
        E+=(2*a-b in Z)
        E+=(2*b-a in Z)
        E+=(((a+b)%2==0) and ((a+b)//2 in Z))
                
print(len(X)*len(Y)*len(Z)-E)

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)

samu = len(R) * len(G) * len(B)

for i in R:
    for j in G:
        k = int(2 * j - i)
        if(0 <= k < n):
            if(s[k] == 'B'):
                samu -= 1
        k = int(2 * i - j)
        if(0 <= k < n):
            if(s[k] == 'B'):
                samu -= 1
        if(i % 2 == j % 2):
            k = int((i + j) / 2)
            if(s[k] == 'B'):
                samu -= 1

print(samu)

Solution (Python):
from itertools import product
from collections import deque
n = int(input())
s = deque(input())
R = []
G = []
B = []
i = 0
while s:
    x = s.popleft()
    if x == 'R':
        R.append(i)
    elif x == 'G':
        G.append(i)
    else:
        B.append(i*2)
    i += 1
ans = len(R)*len(G)*len(B)






B = set(B)
S = product(R, G)
for i, j in S:
    if i + j in B:
        ans -= 1
    if 4 * i - 2*j in B:
        ans -= 1
    if 4 * j - 2 * i in B:
        ans -= 1
print(ans)

Solution (Python):

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

    rs = []
    gs = []
    bs = {}
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.setdefault(i, 1)

    cnt = 0

    for ri in rs:
        for gi in gs:
            df = abs(gi - ri)
            rr = max(gi, ri) + df
            ll = min(gi, ri) - df
            mm = -1
            if df % 2 == 0:
                mm = int((gi + ri) / 2)

            if rr in bs:
                cnt += 1
            if mm in bs:
                cnt += 1
            if ll in bs:
                cnt += 1

    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()

Solution (Python):
import math


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

    rs = []
    gs = []
    bs = {}
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.setdefault(i, 1)

    cnt = 0

    for ri in rs:
        for gi in gs:
            df = abs(gi - ri)
            rr = max(gi,ri) + df
            ll = min(gi,ri) - df
            mm = -1
            if df % 2 == 0:
                mm = int((gi + ri)/2)

            if rr in bs:
                cnt +=1
            if mm in bs:
                cnt+=1
            if ll in bs:
                cnt+=1

            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()

Solution (Python):
n=int(input())
s=str(input())
t=len(s)
r=set()
g=set()
b=set()
a=0
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
d=len(r)*len(g)*len(b)
for ri in r:
    for gi in g:
        rg=gi-ri
        if rg+gi in b:
            d-=1
        if  ri-rg in b:
            d-=1
        if (gi+ri)/2 in b:
            d-=1

print(d)

Solution (Python):
def main():
    N = int(input())
    S = input()
    from collections import Counter
    c = Counter(S)
    result = 1
    if len(list(c.values())) < 3:
        print(0)
        return
    for v in c.values():
        result *= v
    piyo = (N-3) // 2
    for p in range(piyo+1):
        for i in range(N-1 - 2*p -2 + 1):
            a, b, c = S[i] , S[i+p+1] , S[i + (p+1)*2]
            if a != b and a != c and b != c:
                result -= 1
    print(result)
main()

Solution (Python):
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)

Solution (Python):

def main():
    n = int(input())
    s = str(input())
    cnt = 0
    r = s.count('R')
    g = s.count('G')
    b = s.count('B')

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

            cnt +=1

    all = r * g * b
    print(all - cnt) 
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
r, g, b = set(), set(), set()
for i in range(n):
    if s[i] == "R":
        r.add(i)
    elif s[i] == "G":
        g.add(i)
    else:
        b.add(i)

ans = len(r)*len(b)*len(g)
for i in r:
    for j in g:
        chk = j - i
        if chk + j in b:
            ans -= 1
        if i - chk in b:
            ans -= 1
        if (i + j)/2  in b:
            ans -= 1

print(ans)

Solution (Python):
import bisect


def count(text, s, rgb_bag):
    ans = 0
    for i in rgb_bag[text[0]]:
        jst = bisect.bisect_left(rgb_bag[text[1]], i)
        if jst == len(rgb_bag[text[1]]):
            continue
        for j in rgb_bag[text[1]][jst:]:
            k = bisect.bisect_left(rgb_bag[text[2]], j)
            ans += len(rgb_bag[text[2]]) - k
            ng_idx = j + (j - i)
            if ng_idx >= len(s):
                continue
            if s[ng_idx] == text[2]:
                ans -= 1
    return ans


def main():
    n = int(input())
    s = input()
    rgb_bag = {'R': [], 'G': [], 'B': []}
    for i, v in enumerate(s):
        rgb_bag[v].append(i)
    ans = 0

    for r in 'RGB':
        for g in 'RGB':
            for b in 'RGB':
                if r == g or g == b or b == r:
                    continue
                ans += count(r+g+b, s, rgb_bag)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect


def count(text, s, rgb_bag):
    ans = 0
    for i in rgb_bag[text[0]]:
        for j in rgb_bag[text[1]]:
            if i > j:
                continue
            k = bisect.bisect_left(rgb_bag[text[2]], j)
            ans += len(rgb_bag[text[2]]) - k
            ng_idx = j + (j - i)
            if ng_idx >= len(s):
                continue
            if s[ng_idx] == text[2]:
                ans -= 1
    return ans


def main():
    n = int(input())
    s = input()
    rgb_bag = {'R': [], 'G': [], 'B': []}
    for i, v in enumerate(s):
        rgb_bag[v].append(i)
    ans = 0

    for r in 'RGB':
        for g in 'RGB':
            for b in 'RGB':
                if r == g or g == b or b == r:
                    continue
                ans += count(r+g+b, s, rgb_bag)
    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-2):
    for j in range(i+1, N-1):
        k = 2*j - i
        if not k in range(N):
            continue
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            cnt += 1

print(r*g*b-cnt)

Solution (Python):
n = int(input())
s = str(input())
r = s.count('R')
g = s.count('G')
b = s.count('B')
ans = r*g*b

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

Solution (Python):
n = int(input())
string = input()
r = 0
g = 0
b = 0
for c in string:
    if c == 'R':
        r += 1
    elif c == 'G':
        g += 1
    else:
        b += 1
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        if 2*j - i >= n:
            break
        
        if string[i] != string[j] and string[i] != string[2*j-i] and string[j] != string[2*j-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-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[i] != S[2*j-i]:
            ans -=1

print(ans)

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

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

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

Solution (Python):
from collections import Counter
n = int(input())
S = list(input())
C = Counter(S)
equ = 0
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 + 2 * i] != S[j]:
            equ += 1
print(C['R'] * C['G'] * C['B'] - equ)

Solution (Python):
def main():
    N = int(input())
    S = input()
    a = [0, 0, 0]
    for i in range(N):
        if S[i] == 'R':
            a[0] += 1
        elif S[i] == 'G':
            a[1] += 1
        else:
            a[2] += 1
    count = a[0] * a[1] * a[2]
    for i in range(N - 1):
        for j in range(1, N):
            if i - j < 0 or N <= i + j:
                break
            if S[i - j] != S[i] and S[i] != S[i + j] and S[i - j] != S[i + j]:
                count -= 1
    print(count)


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())
 
n = I()
s = list(S())

r = s.count('R')
g = s.count('G')
b = s.count('B')
 
ans = r*g*b
for i in range(n-1):
    for j in range(1,n):
        if i-j<0 or n<=i+j:
            break
        else:
            if s[i-j]!=s[i] and s[i]!=s[i+j] and s[i-j]!=s[i+j]:
                ans-=1
 
print(ans)

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

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

print(ans)

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

Solution (Python):
N = int(input())
S = input()
color = [[], [], []]
for i in range(N):
    if S[i] == 'R':
        color[0].append(i)
    elif S[i] == 'G':
        color[1].append(i)
    elif S[i] == 'B':
        color[2].append(i)
rn = len(color[0])
gn = len(color[1])
bn = len(color[2])
mi = 0
for i in range(N):
    p = 0
    while i + 2*p <= N-1:
        if S[i] != S[i+p] and S[i+p] != S[i+2*p] and S[i] != S[i+2*p]:
           mi += 1
        p += 1
print(rn*gn*bn-mi)

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

cnt = 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]):
            cnt -= 1
print(cnt)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import Counter 
from itertools import combinations
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    d = Counter(S)
    sg = d['R']*d['G']*d['B']
    cnt = 0
    for i, j in combinations([int(i) for i in range(N)], 2):
        if (i+j)%2 == 0:
            m = (i+j)//2
            if (S[i]!=S[j]) and (S[j]!=S[m]) and (S[m]!=S[i]):
                cnt += 1
    print(sg-cnt)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import Counter 
from itertools import combinations
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    d = Counter(S)
    sg = d['R']*d['G']*d['B']
    cnt = 0
    for i, j in combinations([int(i) for i in range(N)], 2):
        if (i+j)%2 == 0:
            m = (i+j)//2
            if (S[i]!=S[j]) and (S[j]!=S[m]) and (S[m]!=S[i]):
                cnt += 1
    print(sg-cnt)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import Counter 
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    d = Counter(S)
    sg = d['R']*d['G']*d['B']
    cnt = 0
    for i in range(N):
        for j in range(i+1, N):
            if (i+j)%2 == 0:
                m = (i+j)//2
                if (S[i]!=S[j]) and (S[j]!=S[m]) and (S[m]!=S[i]):
                    cnt += 1
    print(sg-cnt)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import Counter 
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    d = Counter(S)
    sg = d['R']*d['G']*d['B']
    cnt = 0
    for i in range(N):
        for j in range(i+1, N):
            if (i+j)%2 == 0:
                m = (i+j)//2
                if (S[i]!=S[j]) and (S[j]!=S[m]) and (S[m]!=S[i]):
                    cnt += 1
    print(sg-cnt)
if __name__ == '__main__':
    main()

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

stoi = {'R':0, 'G':1, 'B':2}
itos = {1:'R', 2:'G', 3:'B'}
li = [[0] * N for _ in range(3)]
ans = 0

for ind, s in enumerate(S):
    if(ind == 0):
        li[stoi[s]][ind] = 1
    else:
        for i in range(3):
            li[i][ind] = li[i][ind - 1]
        li[stoi[s]][ind] += 1

for i in range(N):
    for j in range(i + 1, N):
        if(S[i] == S[j]):
            continue
        k = 2 * j - i
        kc = 6 - (stoi[S[i]] + stoi[S[j]] + 2)
        res = li[kc-1][-1] - li[kc-1][j]
        if(k < N and kc == stoi[S[k]] + 1):
            res -= 1
        ans += res

print(ans)

Solution (Python):
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)

Solution (Python):
N = int(input())
S = str(input())
ans = 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 and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1
print(ans)

Solution (Python):
_input = input
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)





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

RGB_num=[0]*3

for c in S:
	if c=='R':
		RGB_num[0] += 1
	elif c=='G':
		RGB_num[1] += 1
	else:
		RGB_num[2] += 1


joken2=0

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

joken1=RGB_num[0]*RGB_num[1]*RGB_num[2]

print(joken1-joken2)

input = _input

Solution (Python):
import sys
import math
import time
import itertools
from collections import defaultdict
import collections
from functools import reduce

def ep(*params):
    print(*params,file=sys.stderr)

start = time.time()

r = 0
g = 0
b = 0

ans = 0
total = 0
N = int(input())
inp = input().rstrip()
for i in range(N):
    c = inp[i]
    if c == 'R':
        r += 1
    elif c == 'G':
        g += 1
    elif c == 'B':
        b += 1
    else:
        assert False
total = r*g*b
ep(r,g,b,total)
ep(time.time() - start)
ans = total
for i in range(0,N-2):
    for j in range(i+1, (N+i+1)//2):
        k = j+j-i
        if inp[i] != inp[j] and inp[j] != inp[k] and inp[k] != inp[i]:
            ans -= 1
ep(time.time() - start)
ep(total, ans)
print(ans)

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


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=input()
dict={}
dict["R"]=[]
dict["G"]=[]
dict["B"]=[]
for i in range(n):
  if s[i]=="R":
    dict["R"].append(i)
  elif s[i]=="G":
    dict["G"].append(i)
  elif s[i]=="B":
    dict["B"].append(i)
  else:
    raise Exception
res=len(dict["R"])*len(dict["G"])*len(dict["B"])
for r in dict["R"]:
  for g in dict["G"]:
      if (r+g)%2==0:
        if s[(r+g)//2]=="B":
          res-=1
      try:
        if s[2*r-g]=="B" and 2*r-g>=0:
          	res-=1
      except:pass
      try:
        if s[2*g-r]=="B" and 2*g-r>=0:
          res-=1
      except:pass      
      
print(res)

Solution (Python):
n = int(input())
colors = list(input())
index = [[],[],[]]
ans = 0

for i in range(n):
    if colors[i] == 'R':
        index[0].append(i)
    elif colors[i] == 'G':
        index[1].append(i)
    else:
        index[2].append(i)

ans = len(index[0]) * len(index[1]) * len(index[2])

for i in range(n):
    for j in range(i+1, n):
        k = 2*j - i
        if k < n and colors[i] != colors[j] and colors[j] != colors[k] and colors[k] != colors[i]:
            ans -= 1

print(ans)

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
        s[i] = 1
    elif s[i] == "G":
        g += 1
        s[i] = 2
    else:
        b += 1
        s[i] = 4

cnt = 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] + s[k] == 7:
                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):
    for j in range(i+1, N):
        k = j + j - i
        if k < N and 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= S.count("R")
g = S.count("G")
b= S.count("B")
ans=r*g*b

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

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

S = [0] * (2 * N + 1)
for i in range(N):
    if SS[i] == "R":
        S[i] = 1

    if SS[i] == "G":
        S[i] = 2

    if SS[i] == "B":
        S[i] = 4

cnt = [[0] * 3 for i in range(N)]
if S[0] == 1:
    cnt[0][0] = 1
elif S[0] == 2:
    cnt[0][1] = 1
else:
    cnt[0][2] = 1


for i in range(1, N):
    cnt[i][0] = cnt[i - 1][0]
    cnt[i][1] = cnt[i - 1][1]
    cnt[i][2] = cnt[i - 1][2]
    if S[i] == 1:
        cnt[i][0] += 1

    if S[i] == 2:
        cnt[i][1] += 1

    if S[i] == 4:
        cnt[i][2] += 1


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

            if to == 3:
                ans += cnt[N - 1][2] - cnt[j][2]
                if S[j + j - i] == 4:
                    ans -= 1

            if to == 5:
                ans += cnt[N - 1][1] - cnt[j][1]
                if S[j + j - i] == 2:
                    ans -= 1

            if to == 6:
                ans += cnt[N - 1][0] - cnt[j][0]
                if S[j + j - i] == 1:
                    ans -= 1


print(ans)

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

cnt = 0

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

B = set(B)
b = len(B)  

for i in R:
  for j in G:
    check = 0
    m = min(i, j)
    M = max(i, j)
    r = M - m
    left = m - r
    right = M + r
    if r % 2 == 0:
      center = m + r//2
      if center in B:
        check += 1
    if left in B:
      check += 1
    if right in B:
      check += 1
    cnt += b - check

print(cnt)

Solution (Python):

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

cnt = 0

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

B = set(B)
b = len(B)  

for i in R:
  for j in G:
    check = 0
    m = min(i, j)
    M = max(i, j)
    r = M - m
    left = m - r
    right = M + r
    if r % 2 == 0:
      center = m + r//2
      if center in B:
        check += 1
    if left in B:
      check += 1
    if right in B:
      check += 1
    cnt += b - check

print(cnt)

Solution (Python):
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)

Solution (Python):
import sys

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

n = ni()
s = ns()

ans = s.count('R') * s.count('G') * s.count('B')

for i in range(10000):
    if 2 + 2 * i >= n: break
    for j in range(10000):
        if 2 + 2 * i + j >= n: break
        if not(s[j] == s[i+1+j] or s[i+1+j] == s[2*i+2+j] or s[2*i+2+j] == s[j]):
            ans -= 1

print(ans)

Solution (Python):
N = int(input())
S = input()
from collections import deque
C =[deque([]),deque([]),deque([])]
for i in range(N):
    if S[i] == 'R':
        C[0].append(i)
    elif S[i] == 'G':
        C[1].append(i)
    else:
        C[2].append(i)

count = len(C[0])*len(C[1])*len(C[2])

for i in C[0]:
    for j in C[1]:
        if max(i,j) + abs(j-i)<N and S[max(i,j) + abs(j-i)] == "B":
            count -= 1
        if min(i,j) - abs(j-i)>=0 and S[min(i,j) - abs(j-i)] == "B":
            count -= 1
        if (i + j)%2 == 0 and S[(i + j)//2] == "B":
            count -= 1


print(count)

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

ans = 0
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 
  
  
  for j in range(i+1,N-1):
     i1,i2,i3 = i,j,j+(j-i)
     if(i3 <= N-1):
       tmp = [S[i1],S[i2],S[i3]]
       tmp.sort()
       tmp = tmp[0]+tmp[1]+tmp[2]
       if(tmp == 'BGR'):
         ans -= 1

ans += r*g*b
print(ans)

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

r, g, b = set(), set(), set()
for i in range(len(s)):
    if s[i] == "R":
        r.add(i)
    elif s[i] == "G":
        g.add(i)
    else:
        b.add(i)
        
ans = len(r) * len(g) * len(b)
for r_idx in r:
    for g_idx in g:
        j, i = max(r_idx, g_idx), min(r_idx, g_idx)
        diff_ji = abs(j-i)
        cnt = 0
        for v in [j + diff_ji, i - diff_ji, (j + i) / 2]:
            if v in b:
                cnt += 1            
        ans -= cnt
print(ans)

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


ans = S.count('R') * S.count('B') * S.count('G')





for i in range(0, 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)

Solution (Python):
n=int(input())
s=input()
r,g,b=0,0,0
for ss in s:
  if ss=="R":r+=1
  elif ss=="G":g+=1
  elif ss=="B":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 not(s[i]==s[j] or s[j]==s[k] or s[i]==s[k]):
      ans-=1
print(ans)