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 = list(input())
r, g, b = 0, 0, 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 - 2):
    k = i + 2
    while True:
        if k >= n:
            break
        j = i + (k - i) // 2
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
        k += 2
print(ans)

Solution (Python):
N = int(input())
S = input()
acc0 = [0]
acc1 = [0]
acc2 = [0]

for i in range(N):
    if S[i]=='R':
        acc0.append(acc0[-1]+1)
        acc1.append(acc1[-1])
        acc2.append(acc2[-1])
    elif S[i]=='G':
        acc0.append(acc0[-1])
        acc1.append(acc1[-1]+1)
        acc2.append(acc2[-1])
    else:
        acc0.append(acc0[-1])
        acc1.append(acc1[-1])
        acc2.append(acc2[-1]+1)
        
ans = 0

for i in range(N):
    for j in range(i+1, N):
        if S[i]==S[j]:
            continue
        
        if (S[i]=='R' and S[j]=='G') or (S[i]=='G' and S[j]=='R'):
            ans += acc2[N]-acc2[j+1]
            
            if j+(j-i)<N and S[j+(j-i)]=='B':
                ans -= 1
                
        elif (S[i]=='R' and S[j]=='B') or (S[i]=='B' and S[j]=='R'):
            ans += acc1[N]-acc1[j+1]
            
            if j+(j-i)<N and S[j+(j-i)]=='G':
                ans -= 1
                
        elif (S[i]=='G' and S[j]=='B') or (S[i]=='B' and S[j]=='G'):
            ans += acc0[N]-acc0[j+1]
            
            if j+(j-i)<N and S[j+(j-i)]=='R':
                ans -= 1

print(ans)

Solution (Python):
from collections import Counter

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

c = Counter(s)

dbl = c["R"] * c["B"] * c["G"]
for l in range(1, n):
    for i in range(n):
        j = i + l
        k = j + l
        if j < n and k < n:
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                dbl -= 1

print(dbl)

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

Solution (Python):
n = int(input())
s = input()
red = []
green = []
blue = [False for _ in range(n)]
b = 0
for i,x in enumerate(s):
    if x == "R":
        red.append(i)
    if x == "G":
        green.append(i)
    if x == "B":
        blue[i] = True
        b += 1

ans = 0
for i in red:
    for j in green:
        p = 0
        if 0<=2*j-i<n and blue[2*j-i]:
            p += 1
        if 0<=2*i-j<n and blue[2*i-j]:
            p += 1
        if (i+j)%2 == 0 and blue[(i+j)//2]:
            p += 1
        ans += b-p
print(ans)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n = inp()
    s = list(input())
    r = [0 for _ in range(n+1)]
    g = [0 for _ in range(n+1)]
    b = [0 for _ in range(n+1)]
    R = []
    G = []
    B = []
    for i in range(n):
        r[i+1] = r[i]
        g[i+1] = g[i]
        b[i+1] = b[i]
        if s[i] == 'R':
            r[i+1] += 1
            R.append(i)
        if s[i] == 'G':
            G.append(i)
            g[i+1] += 1
        if s[i] == 'B':
            B.append(i)
            b[i+1] += 1
    ans = len(R)*len(G)*len(B)
    for i in range(n-2):
        for e in range(1,(n//2)+3):
            if i+2*e >= n:
                continue
            if s[i] != s[i+e] and s[i] != s[i+2*e] and s[i+e] != s[i+e*2]:
                ans -= 1
    print(ans)

    
if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter

(N,) = [int(x) for x in input().split()]
S = input()


count = 0
for j in range(1, N):
    x = S[j]

    leftCount = Counter(S[j + 1 :])
    del leftCount[x]
    rightCount = Counter(S[:j])
    del rightCount[x]

    for k1, v1 in leftCount.items():
        for k2, v2 in rightCount.items():
            if k1 != k2:
                count += v1 * v2

    symmetricCount = 0
    for l in range(1, N):
        if j - l >= 0 and j + l < N:
            y = S[j - l]
            z = S[j + l]
            if x != y and y != z and x != z:
                symmetricCount += 1
        else:
            break

    count -= symmetricCount
print(count)

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def f(g,b):
        left = bisect.bisect_left(g,j)
        right = len(b)-bisect.bisect_left(b,j)
        res = left*right
        for i in range(len(g)):
            li = g[i]
            k = bisect.bisect_left(b,j2-li)
            if k < len(b) and b[k]+li == j2 and b[k] > li:
                res -= 1
        return res

    n = I()
    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)
    ans = 0
    for j in range(1,n-1):
        sj = s[j]
        j2 = 2*j
        if sj == "R":
            ans += f(g,b)+f(b,g)
        elif sj == "G":
            ans += f(r,b)+f(b,r)
        else:
            ans += f(r,g)+f(g,r)
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
N=int(input())
S=input()
l=[0,0,0]
a=0
for i in S:
    if i=="R":
        l[0]+=1
        a+=l[1]*l[2]
    elif i=="G":
        l[1]+=1
        a+=l[0]*l[2]
    else:
        l[2]+=1
        a+=l[0]*l[1]
for i in range(1,2000):
    for j in range(N-2*i):
        if S[j]!=S[i+j] and S[j+i]!=S[j+i+i] and S[j]!=S[j+i+i]:
            a-=1
print(a)

Solution (Python):
from collections import *
from heapq import *
from itertools import *
from fractions import gcd

from decimal import *
import copy
from bisect import *
import time
import sys
import math
input=lambda :sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
N=int(input())
S=input()
lst=[[],[],[]]
for i in range(N):
    if S[i]=="R":
        lst[0].append(i)
    if S[i]=="G":
        lst[1].append(i)
    if S[i]=="B":
        lst[2].append(i)

Z=len(lst[0])*len(lst[1])*len(lst[2])
setlst=[set(i) for i in lst]
for x,y,z in list(permutations(range(3))):
    for a in lst[x]:
        for b in lst[y]:
            if a>b:
                continue
            if b-a + b in setlst[z]:
                Z-=1
print(Z)

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

Solution (Python):

N = int(input())

S = input()

ans = 0


sumR = [0]
sumG = [0]
sumB = [0]

for s in S:
    if s == "R":
        sumR.append(sumR[-1] + 1)
        sumG.append(sumG[-1])
        sumB.append(sumB[-1])
    elif s == "G":
        sumR.append(sumR[-1])
        sumG.append(sumG[-1] + 1)
        sumB.append(sumB[-1])
    elif s == "B":
        sumR.append(sumR[-1])
        sumG.append(sumG[-1])
        sumB.append(sumB[-1] + 1)


for i, s in enumerate(S):
    not_cnt = 0
    for k in range(1, min(i + 1, N - i)):
        L, R = S[i - k], S[i + k]
        if s != L and s != R and L != R:
            not_cnt += 1

    cnt = 0
    if s == "R":
        cnt += sumG[i] * (sumB[-1] - sumB[i + 1])
        cnt += sumB[i] * (sumG[-1] - sumG[i + 1])
    elif s == "G":
        cnt += sumR[i] * (sumB[-1] - sumB[i + 1])
        cnt += sumB[i] * (sumR[-1] - sumR[i + 1])
    elif s == "B":
        cnt += sumG[i] * (sumR[-1] - sumR[i + 1])
        cnt += sumR[i] * (sumG[-1] - sumG[i + 1])

    cnt -= not_cnt
    ans += cnt


print(ans)

Solution (Python):
n = int(input())
s = input()
r = [0] * (n + 1)
g = [0] * (n + 1)
b = [0] * (n + 1)
for i in range(n):
    r[i + 1] = r[i]
    g[i + 1] = g[i]
    b[i + 1] = b[i]
    if s[i] == 'R':
        r[i + 1] += 1
    elif s[i] == 'G':
        g[i + 1] += 1
    else:
        b[i + 1] += 1
ans = 0
for i, c1 in enumerate(s):
    for j in range(i + 1, n):
        c2 = s[j]
        if c1 == c2:
            continue
        if c1 != 'R' and c2 != 'R':
            if (j - i) % 2:
                ans += r[j + 1] - r[i]
            else:
                ans += r[j + 1] - r[i] - \
                    (r[(i + j) // 2 + 1] - r[(i + j) // 2])
        elif c1 != 'G' and c2 != 'G':
            if (j - i) % 2:
                ans += g[j + 1] - g[i]
            else:
                ans += g[j + 1] - g[i] - \
                    (g[(i + j) // 2 + 1] - g[(i + j) // 2])
        else:
            if (j - i) % 2:
                ans += b[j + 1] - b[i]
            else:
                ans += b[j + 1] - b[i] - \
                    (b[(i + j) // 2 + 1] - b[(i + j) // 2])
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(1,(n+1)//2):
    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)

Solution (Python):
N = int(input())
S = input()
r = 0
g = 0
b = 0
for s in S:
    if s == "R":
        r += 1
    if s == "G":
        g += 1
    if s == "B":
        b += 1
res = r * g * b
for i in range(N):
    for j in range(i + 1, N):
        if S[i] != S[j]:
            if j * 2 - i < N:
                if S[j * 2 - i] != S[i] and S[j * 2 - i] != S[j]:
                    res -= 1
print(res)

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

Solution (Python):
n=int(input())
s=list(input())
ctR=0;ctG=0;ctB=0
for i in range(n):
  if s[i]=='R':
    ctR+=1
  elif s[i]=='G':
    ctG+=1
  else:
    ctB+=1

ct=0
for i in range(n):
  x=s[i]
  for j in range(1,min(i,n-1-i)+1):
    if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i-j]!=s[i+j]:
      ct+=1
print(ctR*ctG*ctB-ct)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: print('Yes' if b else 'No')
YESNO=lambda b: print('YES' if b else 'NO')

def main():
    N=int(input())
    S=list(input())
    c=[0,0,0]
    for i,s in enumerate(S):
        if s=='R':
            c[0]+=1
        elif s=='G':
            c[1]+=1
        else:
            c[2]+=1
    count=0
    for i in range(N):
        for j in range(1,N//2+1):
            if i+j*2>=N:
                continue
            if S[i]!=S[i+j] and S[i]!=S[i+j*2] and S[i+j]!=S[i+j*2]:
                count+=1
    print(c[0]*c[1]*c[2]-count)
                

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations
n = int(input())
s = input()
nrgb = [[0 for i in range(3)] for j in range(n+1)]
for i in range(1,n+1):
  for j in range(3):
    nrgb[i][j] = nrgb[i-1][j]
  if s[i-1] == "R":
    nrgb[i][0] += 1
  elif s[i-1] == "G":
    nrgb[i][1] += 1
  else:
    nrgb[i][2] += 1
ans = 0
for i,j in combinations(range(n-1),2):
  if s[i] == s[j]:
    continue
  jdg = 3
  for sk in s[i],s[j]:
    if sk == "G":
      jdg -= 1
    elif sk == "B":
      jdg -= 2
  ans += nrgb[n][jdg]-nrgb[j][jdg]
  if 2*j-i <= n-1 and s[2*j-i] != s[i] and s[2*j-i] != s[j]:
    ans -= 1
print(ans)

Solution (Python):


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    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+1,N):
            if S[i]!=S[j]:
                diff=j-i
                k=j+diff
                if k<N:
                    if S[i]!=S[k] and S[j]!=S[k]:
                        ans-=1
                        
                        
    print(ans)
                        
                
            

main()

Solution (Python):
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    S = input().rstrip('\n')

    ng = 0
    for i in range(N):
        si = S[i]
        if si == "B":
            continue
        for j in range(i+1, N):
            sj = S[j]
            if (si == "R" and sj == "G") or (si == "G" and sj == "R"):
                k1 = 2*i-j
                if k1 >= 0:
                    if S[k1] == "B":
                        ng += 1
                if i%2 == j%2:
                    k2 = (i+j) // 2
                    if S[k2] == "B":
                        ng += 1
                k3 = 2*j-i
                if k3 < N:
                    if S[k3] == "B":
                        ng += 1
    print(S.count("R") * S.count("G") * S.count("B") - ng)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

n = int(input())
s = input()
c = Counter(s)
if not ('R' in c and 'G' in c and 'B' in c):
    print(0)
else:
    ans = 1
    for key in c:
        ans *= c[key]
    for i in range(1, n - 1):
        l = i - 1
        r = i + 1
        while l >= 0 and r < n:
            if s[l] != s[i] and s[l] != s[r] and s[i] != s[r]:
                ans -= 1
            l -= 1
            r += 1
    print(ans)

Solution (Python):
N=int(input())
S=input()
R=[]
G=[]
B=[]
for i in range(0,N):
    if S[i]=='R':
        R.append(i)
    elif S[i]=='G':
        G.append(i)
    else:
        B.append(i)

ans=len(R)*len(G)*len(B)
for i in range(0,len(R)):
    for j in range(0,len(G)):
        r=R[i]
        g=G[j]
        if N>2*g-r>=0:
            if S[2*g-r]=='B':
                ans-=1

        if N>2*r-g>=0:
            if S[2*r-g]=='B':
                ans-=1

        if (r+g)%2==0 and N>(r+g)//2>=0:
            if S[(r+g)//2]=='B':
                ans-=1

print(ans)

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

ruiseki_r = [0] * (n + 1)
ruiseki_g = [0] * (n + 1)
ruiseki_b = [0] * (n + 1)

for i in range(n):
    ruiseki_r[i + 1] = ruiseki_r[i]
    ruiseki_g[i + 1] = ruiseki_g[i]
    ruiseki_b[i + 1] = ruiseki_b[i]
    if s[i] == "R":
        ruiseki_r[i + 1] += 1
    if s[i] == "G":
        ruiseki_g[i + 1] += 1
    if s[i] == "B":
        ruiseki_b[i + 1] += 1
  
ans = 0
for i in range(n):
    if s[i] == "R":
        ans += ruiseki_g[i] * (ruiseki_b[n] - ruiseki_b[i + 1])
        ans += ruiseki_b[i] * (ruiseki_g[n] - ruiseki_g[i + 1])
    if s[i] == "G":
        ans += ruiseki_r[i] * (ruiseki_b[n] - ruiseki_b[i + 1])
        ans += ruiseki_b[i] * (ruiseki_r[n] - ruiseki_r[i + 1])
    if s[i] == "B":
        ans += ruiseki_r[i] * (ruiseki_g[n] - ruiseki_g[i + 1])
        ans += ruiseki_g[i] * (ruiseki_r[n] - ruiseki_r[i + 1])
for i in range(n):
    if s[i] == "R":
        for length in range(1, n + 10):
            if i - length < 0 or i + length >= n:
                break
            elif s[i - length] == "G" and s[i + length] == "B":
                ans -= 1
            elif s[i - length] == "B" and s[i + length] == "G":
                ans -= 1
                
    if s[i] == "B":
        for length in range(1, n + 10):
            if i - length < 0 or i + length >= n:
                break
            elif s[i - length] == "R" and s[i + length] == "G":
                ans -= 1
            elif s[i - length] == "G" and s[i + length] == "R":
                ans -= 1
                
    if s[i] == "G":
        for length in range(1, n + 10):
            if i - length < 0 or i + length >= n:
                break
            elif s[i - length] == "B" and s[i + length] == "R":
                ans -= 1
            elif s[i - length] == "R" and s[i + length] == "B":
                ans -= 1
print(ans)

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

dic = {"R": [], "G": [], "B": []}

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

for i, s in enumerate(S):
    dic[s].append(i)

Bs = set(dic["B"])

ans = 0
for nr in dic["R"]:
    for ng in dic["G"]:
        if nr < ng:
            d = ng - nr
            nos = [nr - d, ng + d]
        else:
            d = nr - ng
            nos = [ng - d, nr + d]
        
        if (nr-ng)%2 == 0:
            nos.append((nr+ng)//2)
        
        ans += len(Bs)
        for no in nos:
            if no in Bs:
                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):
    mid = s[i]
    for j in range(1, n):
        if i-j >= 0 and i+j <= n-1:
            if mid != s[i-j] and mid != s[i+j] and s[i-j] != s[i+j]:
                ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()
R = s.count("R")
G = s.count("G")
B = n-R-G

lR = 0
lG = 0
lB = 0

rR = R
rG = G
rB = B

ans = 0
for i in range(n):
    if s[i] == "R":
        rR -= 1
        ans += lG*rB
        ans += lB*rG
        lR += 1
    elif s[i] == "G":
        rG -= 1
        ans += lB*rR
        ans += lR*rB
        lG += 1
    else:
        rB -= 1
        ans += lR*rG
        ans += lG*rR
        lB += 1
for i in range(1,n-1):
    x = min(i, n-1-i)
    if s[i] == "R":
        for j in range(1,x+1):
            if (s[i-j] == "G" and s[i+j] == "B") or (s[i-j] == "B" and s[i+j] == "G"):
                ans -= 1
    elif s[i] == "G":
        for j in range(1,x+1):
            if (s[i-j] == "R" and s[i+j] == "B") or (s[i-j] == "B" and s[i+j] == "R"):
                ans -= 1
    else:
        for j in range(1,x+1):
            if (s[i-j] == "R" and s[i+j] == "G") or (s[i-j] == "G" and s[i+j] == "R"):
                ans -= 1

print(ans)

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

r, b, g = 0, 0, 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(1, n // 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]:
            ans -= 1

print(ans)

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

basho=[[],[],[]]

for i in range(n):
    if(s[i]=='R'):
        basho[0].append(i)
    if(s[i]=='G'):
        basho[1].append(i)
    if(s[i]=='B'):
        basho[2].append(i)

ans=1

for i in range(3):
    ans*=len(basho[i])

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

print(ans)

Solution (Python):
n = int(input())
s = input()
L = ["R","G","B"]
C = [[0] * n for i in range(3)]
for ii in range(3):
    c = L[ii]
    if s[-1] == c:
        C[ii][-1] += 1
    for i in range(n-2,-1,-1):
        if s[i] == c:
            C[ii][i] = C[ii][i+1] + 1
        else:
            C[ii][i] = C[ii][i+1]




ans = 0
for ii in range(3):
    for i in range(n):
        for j in range(i+1, n):
            if s[i] == L[ii] and s[j] == L[(ii+1)%3] or \
                s[j] == L[ii] and s[i] == L[(ii+1)%3]:
                ans += C[(ii+2)%3][j]
                if j + (j-i) < n and s[j + (j-i)] == L[(ii+2)%3]:
                    ans -= 1
print(ans)

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

ans = 0

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

print(ans)

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

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 = 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

ans = 0
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)

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()
R=[0]
G=[0]
B=[0]
for i in s:
  R.append(R[-1])
  G.append(G[-1])
  B.append(B[-1])
  if i=="R":R[-1]+=1
  elif i=="G":G[-1]+=1
  else:B[-1]+=1
ans=0
for i in range(n-2):
  for j in range(i+1,n-1):
    if s[i]==s[j]:continue
    if "R" not in [s[i],s[j]]:
      ans+=R[-1]-R[j]
      if j+(j-i)<n:
        if s[j+(j-i)]=="R":ans-=1
    if "G" not in [s[i],s[j]]:
      ans+=G[-1]-G[j]
      if j+(j-i)<n:
        if s[j+(j-i)]=="G":ans-=1
    if "B" not in [s[i],s[j]]:
      ans+=B[-1]-B[j]
      if j+(j-i)<n:
        if s[j+(j-i)]=="B":ans-=1
print(ans)

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

ans = 0
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)

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

a = [0, 0, 0]
for i in s:
    if i == "R":
        a[0] += 1
    elif i == "G":
        a[1] += 1
    else:
        a[2] += 1

ans = a[0] * a[1] * a[2]
for i in range(n):
    for j in range(1, n):
        if i + j * 2 >= n:
            continue
        if len(set([s[i], s[i + j], s[i + 2 * j]])) == 3:
            ans -= 1
print(ans)

Solution (Python):
import sys

stdin = sys.stdin

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

n = ni()
s = ns()

rr = 0
gg = 0
bb = 0
for c in s:
    if c == "R":
        rr += 1
    if c == "G":
        gg += 1
    if c == "B":
        bb += 1
ans = rr*gg*bb
for i in range(n):
    for j in range(i+1, n):
        if s[i] != s[j] and 2*j-i < n and s[i] != s[2*j-i] and s[j] != 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 i in range(N):
    for d in range(N):
        j = i + d
        k = i + 2*d
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)