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

Solution (Python):



import sys
input = sys.stdin.readline

def main():
    N=int(input())
    S=input()
    ret = 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:
                break
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                ret-=1

    print(ret)

if __name__ == '__main__':
    main()

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

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

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

    ans = r * g * b
    snum = tuple(s[:])
    for i in range(1,n-1):
        for j in range(i,n):
            k = j * 2 - i
            if k > n:
                break

            if snum[i-1] != snum[j-1] and snum[i-1] != snum[k-1] and snum[j-1] != snum[k-1]:
                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import collections
import math
n = int(input())
s_list = list(input())
c = collections.Counter(s_list)
ans = c["R"] * c["G"] * c["B"]
for i in range(n):
    for j in range(i + 1, n):
        k = j + j - i
        if k < n:
            if s_list[i] != s_list[j] and s_list[i] != s_list[k] and s_list[j] != s_list[k]:
                ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
r = 0
g = 0
b = 0
for x in s:
    if x == 'R':
        r += 1
    elif x == 'G':
        g += 1
    elif x == 'B':
        b += 1

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

print(r * g * b - ng_cnt)

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

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

    ans = r * g * b
    snum = tuple(s[:])
    for i in range(1,n-1):
        for j in range(i,n):
            k = j * 2 - i
            if k > n:
                break

            if snum[i-1] != snum[j-1] and snum[i-1] != snum[k-1] and snum[j-1] != snum[k-1]:
                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
r = 0
g = 0
b = 0
for x in s:
    if x == 'R':
        r += 1
    elif x == 'G':
        g += 1
    elif x == 'B':
        b += 1

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

print(r * g * b - ng_cnt)

Solution (Python):
from collections import Counter

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

a = s_c["R"] * s_c["B"] * s_c["G"]

for i in range(N):
    for j in range(i + 1, N):
        k = 2 * j - i
        if k >= N:
            break
        elif (S[i] != S[j] and S[i] != S[k] and S[j] != S[k]) and \
            not (j - i != k - j):
            a -= 1
print(a)

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


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


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


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


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


if __name__ == "__main__":
    main()

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

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

impossible = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        delta = j - i
        k = j + delta
        if k >= n:
            break

        pairs = {S[i], S[j], S[k]}
        if len(pairs) == 3:
            impossible += 1

ans = R * G * B - impossible
print(ans)

Solution (Python):
def main():
    N = int(input())
    S = input()
    
    ans = S.count('R') * S.count('G') * S.count('B')
    colors = set(list('RGB'))

    for i in range(N-2):
        for j in range(i+1, i+1+(N-1-i)//2):
            k = j + (j - i)
            if set([S[i], S[j], S[k]]) == colors:
                ans -= 1
            
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N = int(input())
    S = input()
    
    ans = S.count('R') * S.count('G') * S.count('B')
    colors = set(list('RGB'))

    for i in range(N-2):
        for j in range(i+1, i+1+(N-1-i)//2):
            k = j + (j - i)
            if set([S[i], S[j], S[k]]) == colors:
                ans -= 1
            
    print(ans)


if __name__ == "__main__":
    main()

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

N = int(readline())
S = [1 if s == 'R' else 2 if s == 'G' else 0 for s in readline().strip()]


cnt = [[0]*3 for _ in range(N)]
for i in range(N):
    cnt[i][S[i]] = 1
for s in range(3):
    for i in range(1, N):
        cnt[i][s] += cnt[i-1][s]

ans = 0
for j in range(1, N-1):
    c1 = S[j]
    c2 = c1 - 1
    c3 = c1 - 2
    r1c2 = cnt[j-1][c2]
    r2c2 = cnt[-1][c2] - cnt[j][c2]
    r1c3 = cnt[j-1][c3]
    r2c3 = cnt[-1][c3] - cnt[j][c3]
    ans += r1c2*r2c3 + r1c3*r2c2
for i in range(N):
    for j in range(1, N):
        if i+2*j >= N:
            break
        if len(set([S[i], S[i+j], S[i+2*j]])) == 3:
            ans -= 1

print(ans)

Solution (Python):
from collections import Counter
def main():
    n = int(input())
    s = input()
    d = Counter(s) 
    ans = d['R'] * d['G'] * d['B']
    cnt = 0
    for i in range(n):
        for j in range(i+1,n): 
            k = 2*j-i 
            if k < n:
                x = s[i]
                y = s[j]
                z = s[k]
                if (x!=y) and (x!=z) and (y!=z):
                    cnt+=1
    
    print(ans-cnt)
main()

Solution (Python):
def remaining(s1, s2):
    if not('R' in [s1, s2]):
        return 'R'
    elif not('G' in [s1, s2]):
        return 'G'
    else:
        return 'B'

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


acc = {
    'R': [0 for _ in range(N)],
    'G': [0 for _ in range(N)],
    'B': [0 for _ in range(N)]
}
acc[S[-1]][-1] = 1
for i in range(N-2, -1, -1):
    acc[S[i]][i] = acc[S[i]][i+1]+1
    if S[i] == 'R':
        acc['G'][i] = acc['G'][i+1]
        acc['B'][i] = acc['B'][i+1]
    elif S[i] == 'G':
        acc['R'][i] = acc['R'][i+1]
        acc['B'][i] = acc['B'][i+1]
    else:
        acc['G'][i] = acc['G'][i+1]
        acc['R'][i] = acc['R'][i+1]


if __name__ == "__main__":
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i]==S[j]:
                continue
            color = remaining(S[i], S[j])
            ans += acc[color][j+1]
            if j+j-i < N:
                if S[j+(j-i)] != S[i] and S[j+(j-i)] != S[j]:
                    ans -= 1
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from itertools import *
from functools import *
from collections import *
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1


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

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











ret = 0
for i in range(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[k] != S[i]:
            ret += 1


ans = r_count * g_count * b_count - ret
print(ans)

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

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

cnt = {'R': 0, 'G': 0, 'B': 0}
for c in S:
    cnt[c] += 1

print(cnt['R'] * cnt['G'] * cnt['B'] - same)

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

R = [X for X, x in enumerate(S) if x == 'R']
G = [Y for Y, y in enumerate(S) if y == 'G']
B = [Z for Z, z in enumerate(S) if z == 'B']

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

cnt=0
for i in range(0,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[i] != S[k] and S[j] != S[k]:
            cnt+=1

print(mae-cnt)

Solution (Python):
from time import time

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

    r = S.count('R')
    g = S.count('G')
    b = S.count('B')
    answer = r * g * b

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

if __name__ == '__main__':
    start = time()
    main()

Solution (Python):
import sys
input = sys.stdin.readline
from itertools import *
from functools import *
from collections import *
from heapq import heapify, heappop, heappush, heappushpop
from numba import njit

INF = float('inf')
NIL = - 1


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

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

ret = 0
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]:
            ret += 1

ans = r_count * g_count * b_count - ret
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
    
ct = 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[i] != s[k] and s[j] != s[k]:
            ct += 1                

print(r*g*b-ct)

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
    


count = 0
for step in range(1,2000):
    start = 0
    while (start + step * 2) < N:
        i,j,k = start, start+step, start+step*2
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            count += 1
        start += 1



print(R*G*B - count)

Solution (Python):


def solve():

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


if __name__ == "__main__":
    solve()

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 diff in range(n):
        j = i + diff
        k = i + 2*diff
        if k >= n:
            break
        elif 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()

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

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

r,g,b=0,0,0
for s in S:
    if s=='R': r+=1
    elif s=='G': g+=1
    else: b+=1


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

ans=r*g*b
print(ans-ng)

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

r = 0
r_l = []
g = 0
g_l = []
b = 0
b_l = []

for i in range(n):
    if s[i] == "R":
        r += 1
        r_l.append(i)
    if s[i] == "G":
        g += 1
        g_l.append(i)
    if s[i] == "B":
        b += 1
        b_l.append(i)
        
ans = r*g*b

for i in r_l:
    for j in g_l:
        tmp = abs(i-j)
        if 0 <= i-tmp and s[i-tmp] == "B":
            ans -= 1
        if 0 <= j-tmp and s[j-tmp] == "B":
            ans -= 1
        if i+tmp < n and s[i+tmp] == "B":
            ans -= 1
        if j+tmp < n and s[j+tmp] == "B":
            ans -= 1

        if (tmp / 2).is_integer():
            if s[max(i,j)-(tmp//2)] == "B":
                ans -= 1
            if max(i,j)-(tmp//2) != min(i,j)+(tmp//2) and s[min(i,j)+(tmp//2)] == "B":
                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):
    t = 1
    while i + 2 * t < N:
        if(S[i] != S[i+t] and S[i] != S[i+2*t] and S[i+t] != S[i+2*t]):
            ans -= 1
        t += 1
print(ans)

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


allcnt = S.count("R") * S.count("G") * S.count("B")


rem = 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[j] != S[k] and S[k] != S[i]:
      rem += 1
print(allcnt - rem)

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

rcnt=s.count("R")
gcnt=s.count("G")
bcnt=s.count("B")
sum=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]:
            sum-=1
print(sum)

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

rcnt=s.count("R")
gcnt=s.count("G")
bcnt=s.count("B")
sum=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]:
            sum-=1
print(sum)

Solution (Python):
n = int(input())
s = str(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)
  else:
    B.append(i+1)

  
ans = 0

for i in range(len(R)):
  for j in range(len(G)):
    temp = 0
    if 2*R[i] - G[j] >0 and 2*R[i] - G[j] <= n:
      if s[2*R[i] - G[j] -1] == "B":
        temp += 1
    if 2*G[j] - R[i] >0 and 2*G[j] - R[i] <= n:
      if s[2*G[j] - R[i]-1] == "B":
        temp += 1
    if (R[i]+G[j])%2 == 0:
      if s[(R[i]+G[j])//2-1] == "B":
        temp += 1
    ans += len(B)- temp

print(ans)

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

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

comb = r*g*b

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

print(comb)

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

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

total = r*g*b

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

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

rc = 0
gc = 0
bc = 0

for si in S:
    if si == 'R':
        rc += 1
    elif si == 'G':
        gc += 1
    else:
        bc += 1


bad = 0
for i in range(N):
    for j in range(i+1, N):
        k = j + (j-i)
        if k > N-1:
            
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            
            bad += 1

ans = rc * gc * bc

ans -= bad
print(ans)

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

rc = 0
gc = 0
bc = 0

for si in S:
    if si == 'R':
        rc += 1
    elif si == 'G':
        gc += 1
    else:
        bc += 1


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

ans = rc * gc * bc

ans -= bad
print(ans)

Solution (Python):
def main():
    N = int(input())
    S = list(str(input()))
    sum = S.count("R") * S.count("G") * S.count("B")
    for i in range(0, N-2):
        si = S[i]
        for j in range(i+1, N-1):
            sj = S[j]
            k = 2*j -i  
            if k < N:
                sk = S[k]
                
                if si != sj and si != sk and sj != sk:
                    sum -= 1
    print(sum)

    


main()

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

rgb = input()

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

ans = r * g * b

for i in range(n):
    for k in range(n):
        a = i + k
        b = a + k
        if b >= n:break
        if rgb[i] != rgb[a] and rgb[i] != rgb[b] and rgb[a] != rgb[b]:
            ans -= 1 
print(ans)

Solution (Python):
import math
import collections
import sys

input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]

n=I()
s=input()
r=[]
g=[]
t=[]
for i in range(n):
    if s[i:i+1]=="R":
        r.append(i)
    elif s[i:i+1]=="G":
        g.append(i)
    else:
        t.append(i)
R=len(r)
G=len(g)
T=n-R-G
ans=R*G*T
for i in range(n-1):
    for j in range(i+1,n):
        if s[i]!=s[j]:
            if j<2*i-j<=n-1:
                if s[i]!=s[2*i-j] and s[j]!=s[2*i-j]:
                    ans-=1
            if j<2*j-i<=n-1:
                if s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
                    ans-=1
            if (i+j)%2==0 and j<(i+j)//2 and s[i]!=s[(i+j)//2] and s[j]!=s[(i+j)//2]:
                ans-=1
print(ans)

Solution (Python):
n = int(input())
s = input()
 
r = []
g = []
b = []
 
val = 0
 
for i in range(n):
    if s[i] == "R":
        r.append(i+1)
    elif s[i] == "G":
        g.append(i+1)
    else:
        b.append(i+1)

r = set(r)
g = set(g)
b = set(b)
for i in r:
    for j in g:
        if 2 * j - i in b:
            val +=1
for i in g:
    for j in b:
        if 2 * j - i in r:
            val +=1
for i in b:
    for j in r:
        if 2 * j - i in g:
            val +=1

print(len(r) * len(g) * len(b) - val)

Solution (Python):
from collections import Counter

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

c = Counter(s)
ans = c['R'] * c['G'] * c['B']

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

print(ans)

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

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

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

print(ans)

Solution (Python):
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
if ans==0:
    print(ans)
else:
    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]:
                    ans -= 1
    print(ans)

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

    rcs = [0] * N
    gcs = [0] * N
    bcs = [0] * N

    for i in range(N):
        if S[i] == 'R':
            rcs[i] = 1
        elif S[i] == 'G':
            gcs[i] = 1
        elif S[i] == 'B':
            bcs[i] = 1

    for i in range(1, N):
        rcs[i] += rcs[i - 1]
        gcs[i] += gcs[i - 1]
        bcs[i] += bcs[i - 1]

    result = 0
    for i in range(N):
        if S[i] == 'R':
            for j in range(i + 1, N):
                if S[j] == 'R':
                    continue
                elif S[j] == 'G':
                    result += bcs[N - 1] - bcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'B':
                        result -= 1
                elif S[j] == 'B':
                    result += gcs[N - 1] - gcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'G':
                        result -= 1
        elif S[i] == 'G':
            for j in range(i + 1, N):
                if S[j] == 'G':
                    continue
                elif S[j] == 'R':
                    result += bcs[N - 1] - bcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'B':
                        result -= 1
                elif S[j] == 'B':
                    result += rcs[N - 1] - rcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'R':
                        result -= 1
        elif S[i] == 'B':
            for j in range(i + 1, N):
                if S[j] == 'B':
                    continue
                elif S[j] == 'R':
                    result += gcs[N - 1] - gcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'G':
                        result -= 1
                elif S[j] == 'G':
                    result += rcs[N - 1] - rcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'R':
                        result -= 1
    print(result)


main()

Solution (Python):
import sys
import fractions

def in_ctrl():
    _input = [_.rstrip() for _ in sys.stdin.readlines()]
    
    N = int(_input[0])
    S = _input[1]
    
    return N,S


def out_ctrl(s):
    print(s)


if __name__ == "__main__":
    N,S = in_ctrl()
    
    cnt = 0
    R = list(map(bool,list(map(int,list(S.replace('R','1').replace('G','0').replace('B','0'))))))
    G = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','1').replace('B','0'))))))
    B = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','0').replace('B','1'))))))
    R_cum, G_cum, B_cum = {}, {}, {}
    for i,r in enumerate(R):
        if r:
            for j,g in enumerate(G[i+1:]):
                if g:
                    if i+j+2 not in B_cum:
                        B_cum[i+j+2] = sum(B[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += B_cum[i+j+2] - B[i+2*j+2]
                    else:
                        cnt += B_cum[i+j+2]
            for j,g in enumerate(B[i+1:]):
                if g:
                    if i+j+2 not in G_cum:
                        G_cum[i+j+2] = sum(G[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += G_cum[i+j+2] - G[i+2*j+2]
                    else:
                        cnt += G_cum[i+j+2]
    for i,r in enumerate(G):
        if r:
            for j,g in enumerate(R[i+1:]):
                if g:
                    if i+j+2 not in B_cum:
                        B_cum[i+j+2] = sum(B[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += B_cum[i+j+2] - B[i+2*j+2]
                    else:
                        cnt += B_cum[i+j+2]
            for j,b in enumerate(B[i+1:]):
                if b:
                    if i+j+2 not in R_cum:
                        R_cum[i+j+2] = sum(R[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += R_cum[i+j+2] - R[i+2*j+2]
                    else:
                        cnt += R_cum[i+j+2]
    for i,r in enumerate(B):
        if r:
            for j,g in enumerate(R[i+1:]):
                if g:
                    if i+j+2 not in G_cum:
                        G_cum[i+j+2] = sum(G[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += G_cum[i+j+2] - G[i+2*j+2]
                    else:
                        cnt += G_cum[i+j+2]
            for j,b in enumerate(G[i+1:]):
                if b:
                    if i+j+2 not in R_cum:
                        R_cum[i+j+2] = sum(R[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += R_cum[i+j+2] - R[i+2*j+2]
                    else:
                        cnt += R_cum[i+j+2]
    
    out_ctrl(cnt)

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

if n <= 3:
    print(0)
    exit()

Rs = []
Gs = []
Bs = []

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

    Rs.append(r)
    Gs.append(g)
    Bs.append(b)

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        if S[i] == S[j]:
            continue
        color = {'R', 'G', 'B'}
        color.discard(S[i])
        color.discard(S[j])
        desire = color.pop()

        pattern = 0
        if desire == 'R':
            pattern = Rs[-1] - Rs[j]
        elif desire == 'G':
            pattern = Gs[-1] - Gs[j]
        elif desire == 'B':
            pattern = Bs[-1] - Bs[j]
        delta = j-i
        if j+delta < n:
            if S[j+delta] == desire:
                pattern -= 1
        ans += pattern

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

print(ans)

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

R_i = {}
G_i = {}
B_i = {}

ans = 0
for i in range(n) :
  if s[i] == 'R' :
    R_i[i] = 1
  if s[i] == 'G' :
    G_i[i] = 1
  if s[i] == 'B' :
    B_i[i] = 1

ans = len(R_i)*len(G_i)*len(B_i)

for ri in R_i :
  for gi in G_i :
    
    if 2*ri-gi in B_i :
      ans -= 1
    
    if 2*gi-ri in B_i :
      ans -= 1
    
    if (ri+gi)%2 == 0 :
      if (ri+gi)//2 in B_i :
        ans -= 1
    
print(ans)

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

R_i = {}
G_i = {}
B_i = {}

ans = 0
for i in range(n) :
  if s[i] == 'R' :
    R_i[i] = 1
  if s[i] == 'G' :
    G_i[i] = 1
  if s[i] == 'B' :
    B_i[i] = 1

ans = len(R_i)*len(G_i)*len(B_i)

for ri in R_i :
  for gi in G_i :
    
    if 2*ri-gi in B_i :
      ans -= 1
    
    if 2*gi-ri in B_i :
      ans -= 1
    
    if (ri+gi)%2 == 0 :
      if (ri+gi)//2 in B_i :
        ans -= 1
    
print(ans)

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

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

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

Solution (Python):
from sys import stdin
import sys





N = int(input())
S = list(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)
    else:
        b.append(i+1)



cnt = len(r)*len(g)*len(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[i] != S[k] and S[j] != S[k]:
            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[i] != S[j] and S[k] != S[j] and S[i] != S[k]:
            ans -= 1

print(ans)

Solution (Python):
def main():
  n = int(input())
  s = input()
  r = g = b = d =0
  for i in range(n):
    if s[i] == "R":
      r += 1
    elif s[i] == "G":
      g += 1
    else:
      b += 1
  a = 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
      d += 1
  print(a - d)
  
if __name__ == "__main__":
  main()

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

cnts = [0, 0, 0]
for i in range(n):
    if s[i] == 'R':
        cnts[0] += 1
    elif s[i] == 'G':
        cnts[1] += 1
    else:
        cnts[2] += 1

ans = cnts[0] * cnts[1] * cnts[2]

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

Solution (Python):
n=int(input())
s=input()
c=s.count
a=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]:
      a-=1
print(a)

Solution (Python):









import sys
sys.setrecursionlimit(15000)

n = int(sys.stdin.readline())
s = sys.stdin.readline().rstrip()
d = {"R":[],"G":[],"B":[]}
sb = set()
for i in range(n):
    d[s[i]].append(i)
    if s[i]=="B":
        sb.add(i)
s = 0


for a in d["R"]:
    for b in d["G"]:
        s += len(d["B"])
        if 2*b-a in sb:
            s -= 1
        if 2*a-b in sb:
            s -= 1
        if (a+b)%2 == 0 and (a+b)//2 in sb:
            s -= 1
print(s)