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


def solve(N: int, S: str):
    rest_r = []
    rest_g = []
    rest_b = []

    c_r = 0
    c_g = 0
    c_b = 0

    i_r = []
    i_g = []
    i_b = []

    for i in range(N-1, -1, -1):
        if S[i] == 'R':
            c_r+=1
            i_r.append(i)
        if S[i] == 'G':
            c_g+=1
            i_g.append(i)
        if S[i] == 'B':
            c_b+=1
            i_b.append(i)
        rest_r.append(c_r)
        rest_g.append(c_g)
        rest_b.append(c_b)

    rest_r.reverse()
    rest_g.reverse()
    rest_b.reverse()

    i_r.reverse()
    i_g.reverse()
    i_b.reverse()

    sum = 0

    
    for r in i_r:
        for g in i_g:
            i_max = max(r, g)
            sum += rest_b[i_max]
            i = i_max + abs(g - r)
            if i < N and S[i] == 'B':
                sum -= 1
    for r in i_r:
        for g in i_b:
            i_max = max(r, g)
            sum += rest_g[i_max]
            i = i_max + abs(g - r)
            if i < N and S[i] == 'G':
                sum -= 1

    for r in i_g:
        for g in i_b:
            i_max = max(r, g)
            sum += rest_r[i_max]
            i = i_max + abs(g - r)
            if i < N and S[i] == 'R':
                sum -= 1

    print(sum)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)

if __name__ == '__main__':
    main()

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

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


i=0
while i<N-1:
    j=i+1
    while 2*j-i<N:
        ans-=1*(S[i]!=S[j])*(S[j]!=S[2*j-i])*(S[2*j-i]!=S[i])
        j+=1
    i+=1

print(ans)

Solution (Python):
n = int(input())
s = list(input())
s_reverse = s[::-1]
ans = 0
r_cnt = 0
b_cnt = 0
g_cnt = 0
r = [0] * n
b = [0] * n
g = [0] * n
for i in range(n):
    if s_reverse[i] == 'R':
        r_cnt += 1
    elif s_reverse[i] == 'G':
        g_cnt += 1
    else:
        b_cnt += 1
    r[n-i-1] = r_cnt
    g[n-i-1] = g_cnt
    b[n-i-1] = b_cnt
    
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == 'R' and s[j] == 'G':
            if (2*j - i) < n:
                if s[j+(j-i)] == 'B':
                    ans -= 1
            ans += b[j+1]
        elif s[i] == 'R' and s[j] == 'B':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'G':
                    ans -= 1
            ans += g[j+1]
        elif s[i] == 'G' and s[j] == 'R':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'B':
                    ans -= 1
            ans += b[j+1]
        elif s[i] == 'G' and s[j] == 'B':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'R':
                    ans -= 1
            ans += r[j+1]
        elif s[i] == 'B' and s[j] == 'R':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'G':
                    ans -= 1
            ans += g[j+1]
        elif s[i] == 'B' and s[j] == 'G':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'R':
                    ans -= 1
            ans += r[j+1]

print(ans)

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

n= int(input())
s = input()
R = []
G = []
B = []
import bisect
def func(l1,l2,l3,l3set):
  res =0
  for i in l1:
    for j in l2[bisect.bisect_left(l2,i):]:
      a = l3[bisect.bisect_left(l3,j):]
      res += len(a)
      if j*2-i in l3set:
        res -=1
  return res

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 = set(R)
G_set = set(G)
B_set = set(B)
ans = func(R,G,B,B_set)+func(R,B,G,G_set)+func(G,R,B,B_set)+func(G,B,R,R_set) + func(B,R,G,G_set)+func(B,G,R,R_set)
print(ans)

Solution (Python):
import sys, math

n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()
res = 0

r = []
g = []
b = []
for i, c in enumerate(s):
    if c == "R":
        r.append(i)
    if c == "G":
        g.append(i)
    if c == "B":
        b.append(i)


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

def bin_find(tar):
    left = 0
    right = len(b)
    while left < right:
        mid = (left+right) // 2
        if b[mid] < tar:
            left = mid+1
        elif b[mid] > tar:
            right = mid
        else:
            return mid
    return -1

for i in r:
    for j in g:
        diff = i - j
        if diff < 0:
            diff = -diff
        if diff % 2 == 0:
            target = diff // 2 + min(i,j)
            if target < n and s[target] == 'B':
                res -= 1
        target = min(i,j) - diff
        if target >= 0 and s[target] == 'B':
            res -= 1
        target = max(i,j) + diff
        if target < n and s[target] == "B":
            res -= 1

print(res)

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


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

B = set(B)

ans = 0
for r in R:
    for g in G:
        y = max(r,g)
        x = min(r,g)
        
        

        tmp = int((x+y)/2)
        if (x+y)%2==0 and (tmp in B):
            ans += 1
        
        tmp = 2*y-x
        if tmp in B:
            ans += 1
        
        tmp = 2*x-y
        if tmp in B:
            ans += 1

print(base-ans)

Solution (Python):
from collections import defaultdict

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

rgb = defaultdict(list)
blue = defaultdict(int)

for i in range(n):
    if s[i] == 'R':
        rgb[0].append(i)
    elif s[i] == 'G':
        rgb[1].append(i)
    else:
        blue[i] = i

ans = len(rgb[0]) * len(rgb[1]) * len(blue)

for r in rgb[0]:
    for g in rgb[1]:
        tmp = sorted([r, g])
        if tmp[0] - (tmp[1]-tmp[0]) in blue:
            ans -= 1
        if tmp[1] + (tmp[1]-tmp[0]) in blue:
            ans -= 1
        if (tmp[1]+tmp[0]) % 2 == 0 and (tmp[1]+tmp[0])//2 in blue:
            ans -= 1

print(ans)

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

R = []
G = []
B = []
result = 0

for i in range(N):
    if S[i] == 'R':
        R.append(1)
        G.append(0)
        B.append(0)
    elif S[i] == 'G':
        R.append(0)
        G.append(1)
        B.append(0)
    if S[i] == 'B':
        R.append(0)
        G.append(0)
        B.append(1)
for i in range(N):
    R.append(0)
    G.append(0)
    B.append(0)
R_total = sum(R)
G_total = sum(G)
B_total = sum(B)
R_count = []
G_count = []
B_count = []
for i in range(N):
    if S[i] == 'R':
        R_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    elif S[i] == 'G':
        G_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    if S[i] == 'B':
        B_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == 'R':
            if S[j] == 'G':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'B':
            if S[j] == 'R':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'G':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
print(result)

Solution (Python):
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    N = int(input())
    S = input()[:-1]

    A = [[] for _ in range(3)]
    for i in range(N):
        if S[i:i+1] == "R":
            A[0].append(i)
        if S[i:i+1] == "G":
            A[1].append(i)
        if S[i:i+1] == "B":
            A[2].append(i)

    res = 0
    for P in itertools.permutations(range(3)):
        a = P[0]
        b = P[1]
        c = P[2]
        for i in A[a]:
            x = bisect.bisect_left(A[b], i)
            for j in A[b][x:]:
                res += len(A[c]) - bisect.bisect_left(A[c], j)
    
    for l in range(1, math.ceil(N/2)):
        n = 0
        while n+l*2 < N:
            if S[n] != S[n+l] and S[n+l] != S[n+l*2] and S[n+l*2] != S[n]:
                res -= 1
            n += 1
    
    print(res)


if __name__ == '__main__':
    main()

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

rs = set()
gs = set()
bs = set()
for i, s in enumerate(S):
    if s == 'R':
        rs.add(i+1)
    elif s == 'G':
        gs.add(i+1)
    elif s == 'B':
        bs.add(i+1)

count = 0
for i in rs:
    for j in gs:
        a = min(i, j)
        b = max(i, j)
        d = b - a
        e = 0
        if (a - d) in bs:
            e += 1
        if (b + d) in bs:
            e += 1
        if d % 2 == 0 and (b - (d // 2)) in bs:
            e += 1
        count += (len(bs) - e)
print(count)

Solution (Python):

"""
Created on Sun Apr 12 20:59:07 2020

@author: naoki
"""

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

Dp = [[0 for _ in range(3)] for _ in range(N)]

if S[0] == "R":
    Dp[0][0] = 1
elif S[0] == "G":
    Dp[0][1] = 1
elif S[0] == "B":
    Dp[0][2] = 1

allcount = 0
for j in range(1,N):
    Dp[j][0] = Dp[j-1][0]
    Dp[j][1] = Dp[j-1][1]
    Dp[j][2] = Dp[j-1][2]
    
    if S[j] == "R":
        Dp[j][0] += 1
        allcount += Dp[j-1][1]*Dp[j-1][2]
    elif S[j] == "G":
        Dp[j][1] += 1
        allcount += Dp[j-1][0]*Dp[j-1][2]
    elif S[j] == "B":
        Dp[j][2] += 1
        allcount += Dp[j-1][0]*Dp[j-1][1]
    
for j in range(1,N):
    for i in range(1,j):
        if j -i -i >= 0:
            if S[j-i] != S[j-2*i] and S[j-i] !=S[j] and S[j-2*i] !=S[j]:
                allcount -= 1
                pass
print(allcount)

Solution (Python):
def main():
    n = int(input())
    s = input()
    r, g, b = [], [], []
    for i in range(n):
        t = s[i]
        if t == 'R':
            r.append(i)
        elif t == 'G':
            g.append(i)
        else:
            b.append(i)
    ans = len(r) * len(g) * len(b)

    for i in range(1, n // 2 + 1):
        for k in range(n):
            try:
                if {s[k], s[k + i], s[k + 2 * i]} == {'R', 'G', 'B'}:
                    ans -= 1
            except IndexError:
                break
    print(ans)


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

Solution (Python):


import itertools



def count_triplets(n, s):
    rs = [i for i in range(n) if s[i] == "R"]
    gs = [i for i in range(n) if s[i] == "G"]
    bs = [i for i in range(n) if s[i] == "B"]
    rgbs = [rs, gs, bs]
    gt_rs = [sum(r >= i for r in rs) for i in range(n + 2)]
    gt_gs = [sum(r >= i for r in gs) for i in range(n + 2)]
    gt_bs = [sum(r >= i for r in bs) for i in range(n + 2)]
    gts = [gt_rs, gt_gs, gt_bs]
    m = 3
    res = 0
    for col1, col2 in itertools.combinations(range(m), r=2):
        col3 = m - col1 - col2
        for r, g in itertools.product(rgbs[col1], rgbs[col2]):
            if r >= g:
                r, g = g, r
            res += gts[col3][g + 1]
            if 0 <= 2 * g - r < n:
                res -= gts[col3][2 * g - r]
                res += gts[col3][2 * g - r + 1]
    return res


def main():
    n = int(input())
    s = input()
    res = count_triplets(n, s)
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    colors = ['R', 'G', 'B']
    N = int(input())
    S = input()
    
    ans = 0
    cnts = {'R': [], 'G': [], 'B': []}
    if S[-1] == 'R':
        cnts = {'R': [1], 'G': [0], 'B': [0]}
    elif S[-1] == 'G':
        cnts = {'R': [0], 'G': [1], 'B': [0]}
    else:
        cnts = {'R': [0], 'G': [0], 'B': [1]}

    for i, s in enumerate(S[::-1][1:]):
        for color in colors:
            if s == color:
                cnts[color].append(cnts[color][i] + 1)
            else:
                cnts[color].append(cnts[color][i])

    for color in colors:
        cnts[color] = cnts[color][::-1]

    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == S[j]:
                continue
            oth = colors[:]
            oth.remove(S[i])
            oth.remove(S[j])
            oth = oth[0]
            cnt = cnts[oth][j+1]
            ans += cnt
            if j + (j - i) < N:
                if S[j + (j - i)] == oth:
                    ans -= 1
            
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=input()
L=len(s)
ans=0

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

for i in range(L-2):
    for j in range(i+1,L-1):
        if s[j]!=s[i]:
            if j+j-i<=L-1:
                if s[j+j-i]!=s[i] and s[j+j-i]!=s[j]:
                    ans+=1
print(r*b*g-ans)

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):
        if(j+(j-i) >= N):
            break
        if((S[j+(j-i)] != S[i]) and (S[j+(j-i)] != S[j]) and (S[i] != S[j])):
            cnt += 1
print(r*g*b-cnt)

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

R, G, B = set(), set(), set()
r, g, b = 0, 0, 0

for i, s in enumerate(S):
    if s == "R":
        R.add(i + 1)
        r += 1

    elif s == "G":
        G.add(i + 1)
        g += 1

    else:
        B.add(i + 1)
        b += 1

ans = r * g * b
punish = 0


for rr in R:
    for gg in G:
        if (min(rr, gg) - abs(rr - gg)) in B:
            punish += 1

        if (max(rr, gg) + abs(rr - gg)) in B:
            punish += 1
      
        if (abs(rr - gg) % 2 == 0) and (abs(rr - gg)//2 + min(rr, gg) in B):
            punish += 1
      

print(ans - punish)

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

print(ans)

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

    r=0
    g=0
    b=0
    s=0
    for i,a in enumerate(a_list):
        if a=="R":
            r+=1
            s+=g*b
        elif a=="G":
            g+=1
            s+=r*b
        elif a=="B":
            b+=1
            s+=r*g
    
    for i in range(1,N):
        for j in range(N):
            if j+2*i>=N:
                break

            s1=a_list[j]
            s2=a_list[j+i]
            s3=a_list[j+i*2]
            if s1!=s2:
                if s2!=s3:
                    if s1!=s3:
                        s-=1
    
    print(s)




if __name__ =='__main__': 
    main()

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

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)

count = len(R)*len(G)*len(B)
for i in range(1,N-1):
    f = s[i-1::-1]
    b = s[i+1:]
    for j in range(min(len(f),len(b))):
        if f[j]!=s[i] and f[j] != b[j] and s[i] != b[j]:
            count -=1
print(count)

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

ans = r*g*b

for i in range(n):
    m = min(i, n-i-1)
    for j in range(m+1):
        if s[i] != s[i+j] and s[i] != s[i-j]:
            if s[i+j] != s[i-j]:
                ans -= 1

print(ans)

Solution (Python):
n = int(input())
l = list(input())
r = 0
g = 0
b = 0
for i in range(n):
  r += 1 if l[i] == "R" else 0
  g += 1 if l[i] == "G" else 0
  b += 1 if l[i] == "B" else 0
       
ans = 0
for j in range(n):
  for i in range(j):
    tmp = j-i+j
    if tmp >= n:
      continue
    if l[i] != l[j] and(l[i] != l[tmp]) and(l[j] != l[tmp]):
      ans -= 1
      
print(r * b * g + ans)

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
S = (stdin.readline().rstrip())
r = []
g = []
b = []
cnt = 1
for i in S:
    if i == "R":
        r.append(cnt)
    elif i == "G":
        g.append(cnt)
    else:
        b.append(cnt)
    cnt += 1

ans = len(r)*len(g)*len(b)
for i in r:
    for j in range(1,4001):
        if i + j -1 >= N:
            break
        if i - j -1 < 0:
            break
            
        s1 = S[i+j-1]
        s2 = S[i-j-1]
        if s1 != "R" and s2 != "R" and (s1 != s2):
            ans -= 1
for i in g:
    for j in range(1,4001):
        if i + j - 1 >= N:
            break
        if i - j -1 < 0:
            break
            
        s1 = S[i+j-1]
        s2 = S[i-j-1]
        if s1 != "G" and s2 != "G" and (s1 != s2):
            ans -= 1
for i in b:
    for j in range(1,4001):
        if i + j - 1 >= N:
            break
        if i - j - 1 < 0:
            break
            
        s1 = S[i+j-1]
        s2 = S[i-j-1]
        if s1 != "B" and s2 != "B" and (s1 != s2):
            ans -= 1
print(ans)

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

R = [0] * N
G = [0] * N
B = [0] * N
SR = [0] * N
SG = [0] * N
SB = [0] * N
pr = 0
pg = 0
pb = 0
for i in range(N):
    if S[i] == "R":
        pr += 1
        SR[i] = pr
        R[i] = 1
    elif S[i] == "G":
        pg += 1
        SG[i] = pg
        G[i] = 1
    elif S[i] == "B":
        pb += 1
        SB[i] = pb
        B[i] = 1
        
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if R[i]:
            if G[j]:
                if j+(j-i) <= N-1 and B[j + (j-i)] == 1:
                    ans += SB[N-1] - SB[j] - 1
                else:
                    ans += SB[N-1] - SB[j]
            if B[j]:
                if j+(j-i) <= N-1 and G[j + (j-i)] == 1:
                    ans += SG[N-1] - SG[j] - 1
                else:
                    ans += SG[N-1] - SG[j]
        if G[i]:
            if B[j]:
                if j+(j-i) <= N-1 and R[j + (j-i)] == 1:
                    ans += SR[N-1] - SR[j] - 1
                else:
                    ans += SR[N-1] - SR[j]
            if R[j]:
                if j+(j-i) <= N-1 and B[j + (j-i)] == 1:
                    ans += SB[N-1] - SB[j] - 1
                else:
                    ans += SB[N-1] - SB[j]
        if B[i]:
            if G[j]:
                if j+(j-i) <= N-1 and R[j + (j-i)] == 1:
                    ans += SR[N-1] - SR[j] - 1
                else:
                    ans += SR[N-1] - SR[j]
            if R[j]:
                if j+(j-i) <= N-1 and G[j + (j-i)] == 1:
                    ans += SG[N-1] - SG[j] - 1
                else:
                    ans += SG[N-1] - SG[j]
print(ans)

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

    R, G, B = [], [], []
    for i in range(n):
        if s[i] == 'R':
            R.append(i + 1)
        elif s[i] == 'G':
            G.append(i + 1)
        else:
            B.append(i + 1)

    lb = len(B)
    RB, GB, SB = set(R), set(G), set(B)
    ans = 0

    for i in R:
        for j in G:
            ans += lb
            if j + (j - i) in SB:
                ans -= 1

    for i in G:
        for j in B:
            if j + (j - i) in RB:
                ans -= 1

    for i in B:
        for j in R:
            if j + (j - i) in GB:
                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def calc(N, S):
    id = {'R': 0, 'G': 1, 'B': 2}
    rgb = [[0, 0, 0] for _ in range(N)]
    rgb[0][id[S[0]]] = 1
    for i in range(1, N):
        for j in [0, 1, 2]:
            rgb[i][j] = rgb[i - 1][j]
            if j == id[S[i]]:
                rgb[i][j] += 1
    result = 0
    for i in range(N - 1, -1, -1):
        X = S[i]
        YZ = (['G', 'B'] if X == 'R' else ['R', 'B'] if X == 'G' else ['R', 'G'])
        j = i - 1
        while j > -1:
            if S[j] in YZ:
                result += rgb[j][3 - id[X] - id[S[j]]]
                if 2 * j - i > -1 and not S[2 * j - i] in [X, S[j]]:
                    result -=1
            j -= 1
    return result


N = int(input())
S = input()
print(calc(N, S))

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










def check(i,j,N): 
	
				
              	
			  	
	if (0 > (2*j-i) or (2*j-i) >= N):
		
		return False
	else:
		return (S[j] != S[2*j-i] and S[2*j-i] != S[i])
		



n_equal = 0
for i in range(N):
	for j in range(i+1, int((N+i)/2)+1):
		if( S[i]==S[j]):
			continue
		else:
			if(check(i,j,N)==True):
				n_equal+=1
				
				
				



n_R = S.count('R')
n_G = S.count('G')
n_B = S.count('B')

n_all = n_R*n_G*n_B


print(n_all-n_equal)

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

r_total = s.count("R")
g_total = s.count("G")
b_total = s.count("B")

r_c = []
g_c = []
b_c = []

for moji in s:
  if moji == "R":
    r_total -= 1
  if moji == "G":
    g_total -= 1
  if moji == "B":
    b_total -= 1
  r_c.append(r_total)
  g_c.append(g_total)
  b_c.append(b_total)

color_dic = {"R": r_c, "G": g_c, "B": b_c}

  
from itertools import combinations as cmb
counter = 0
RGB = set(["R", "G", "B"])
for i, j in cmb(list(range(n)), 2):
  
  if s[i] != s[j]:
    rest_color = list(RGB - set([s[i], s[j]]))[0]
    
    counter += color_dic[rest_color][j]
    try:
      if s[2 * j - i] == rest_color:
        counter -= 1
    except IndexError:
      pass
print(counter)

Solution (Python):
n = input()
s = raw_input().strip()

pre = [[0 for i in xrange(3)] for j in xrange(n + 1)]

for i in xrange(n):
    for j in xrange(3): pre[i + 1][j] = pre[i][j]

    if s[i] == 'R': pre[i + 1][0] += 1
    elif s[i] == 'G': pre[i + 1][1] += 1
    else: pre[i + 1][2] += 1

collist = ['R', 'G', 'B']

ans = 0
for j in xrange(1, n):
    for k in xrange(j + 1, n):
        if s[j] == s[k]: continue
        
        it = 2
        if 'R' not in (s[j], s[k]): it = 0
        elif 'G' not in (s[j], s[k]): it = 1

        ans += pre[j][it]

        i = j + j - k

        if i >= 0:
            if s[i] == collist[it]: ans -= 1

print ans

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

Solution (Python):
N = int(input())
S = input()
R = 0
B = 0
G = 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+1,N):
        k = 2*j-i
        if k >= N:
            continue
        elif S[i]!= S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)

Solution (Python):
import bisect
n = int(input())
s = input()
rl = []
gl = []
bl = []
for i in range(n):
    if s[i] == "R": rl.append(i+1)
    elif s[i] == "G": gl.append(i+1)
    else: bl.append(i+1)

ans = len(rl)*len(gl)*len(bl)

for i in rl:
    for j in gl:
        lb = len(bl)
        if (i+j)%2==0:
            index = bisect.bisect_left(bl, (i+j)//2)
            if lb>index and bl[index] == ((i+j)//2):
                ans -= 1
        if i>j:
            index = bisect.bisect_left(bl, 2*i-j)
            if lb>index and bl[index] == (2*i-j):
                ans -= 1
            if 2*j-i>0:
                index = bisect.bisect_left(bl, 2*j-i)
                if lb>index and bl[index] == (2*j-i):
                    ans -= 1
        elif i<j:
            index = bisect.bisect_left(bl, 2*j-i)
            if lb>index and bl[index] == (2*j-i):
                ans -= 1
            if 2*i-j>0:
                index = bisect.bisect_left(bl, 2*i-j)
                if lb>index and bl[index] == (2*i-j):
                    ans -= 1

print(ans)

Solution (Python):
N = int(input())
A = input()
ans = 0
R = 0
G = 0
B = 0
for i in A:
  if i == 'R':
    R += 1
  elif i == 'G':
    G += 1
  elif i == 'B':
    B += 1
ans = R*G*B
for i in range(0,N-2):
  for j in range(i+1,N-1):
    k = 2*j -i
    if k < N:
      if A[i] != A[j] and A[j] != A[k] and A[k] != A[i]:
        ans -= 1
print(ans)

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

r = []
g = []
b = set()

for i in range(n):
  if s[i] == 'R':
    r.append(i + 1)
  elif s[i] == 'G':
    g.append(i + 1)
  else:
    b.add(i + 1)

summary = 0
for i in r:
  for j in g:
    min_value = min(i, j)
    max_value = max(i, j)
    count = 0
    dif = max_value - min_value
    if min_value - dif in b:
      count += 1
    if max_value + dif in b:
      count += 1
    if max_value - (dif / 2) in b:
      count += 1
    summary += len(b) - count

print(summary)

Solution (Python):
n = int(input())
s = input()
pos = {
    "R": [],
    "G": [],
    "B": []
}
for i in range(n):
    pos[s[i]].append(i)

count = {
    "R": [0] * n,
    "G": [0] * n,
    "B": [0] * n
}
for i in range(n - 2, -1, -1):
    prevC = s[i + 1]
    for c in "RGB":
        count[c][i] = count[c][i + 1]
    count[prevC][i] += 1


ans = 0
for i in range(n):
    if s[i] == "R":
        for p in pos["G"]:
            if p > i:
                d = p - i
                ans += count["B"][p]
                if p + d < n and s[p + d] == "B":
                    ans -= 1
        for p in pos["B"]:
            if p > i:
                d = p - i
                ans += count["G"][p]
                if p + d < n and s[p + d] == "G":
                    ans -= 1

    if s[i] == "G":
        for p in pos["R"]:
            if p > i:
                d = p - i
                ans += count["B"][p]
                if p + d < n and s[p + d] == "B":
                    ans -= 1
        for p in pos["B"]:
            if p > i:
                d = p - i
                ans += count["R"][p]
                if p + d < n and s[p + d] == "R":
                    ans -= 1

    if s[i] == "B":
        for p in pos["G"]:
            if p > i:
                d = p - i
                ans += count["R"][p]
                if p + d < n and s[p + d] == "R":
                    ans -= 1
        for p in pos["R"]:
            if p > i:
                d = p - i
                ans += count["G"][p]
                if p + d < n and s[p + d] == "G":
                    ans -= 1
print(ans)

Solution (Python):
from collections import defaultdict, Counter
from heapq import heapify, heappop, heappush
from sys import stdin


def main2():
    res = 0
    n = int(input())
    s = str(input())
    for i in range(n):
        for j in range(i+1, n):
            if s[i] == s[j]:
                continue
            else:
                for k in range(j+1, n):
                    if s[i] == s[k] or s[j] == s[k]:
                        continue
                    if j - i == k - j:
                        continue
                    res += 1
    print(res)


def main():
    res = 0
    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"]

    for i in range(n):
        if s[i] == "R":
            res += sum(x > i for x in g) * sum(x > i for x in b)
        elif s[i] == "G":
            res += sum(x > i for x in r) * sum(x > i for x in b)
        elif s[i] == "B":
            res += sum(x > i for x in r) * sum(x > i for x in g)
        for j in range(i+1, n):
            k = (j-i)+j
            if k >= n:
                continue
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                res -= 1
    print(res)


def input(): return stdin.readline().rstrip()


main()

Solution (Python):
import collections
import bisect
N = int(input())
S = input()
ans = 0
table = collections.defaultdict(list)
for i, x in enumerate(S):
    table[x].append(i)
R = len(table["R"])
G = len(table["G"])
L = len(table["B"])

s = set(table["B"])

for i in range(R):
    for j in range(G):
        x = table["R"][i]
        y = table["G"][j]
        if x > y:
            x, y = y, x
        ans += L
        if 2*y - x in s:
            ans -= 1
        if 2*x - y in s:
            ans -= 1
        if (x+y) % 2 == 0 and (x+y)//2 in s:
            ans -= 1

print(ans)

Solution (Python):
import bisect

def d162(n , sli):

    ans = 0
    Rli = []
    Gli = []
    Bli = []

    for i in range(n):
        if sli[i] == "R":
            Rli.append(i)
        elif sli[i] == "G":
            Gli.append(i)
        else:
            Bli.append(i)

    Rcount = len(Rli)
    Gcount = len(Gli)
    Bcount = len(Bli)
    Rli.sort()
    Gli.sort()
    Bli.sort()

    for i in range(n - 3):
        for w in range(i + 1 , n):
            if sli[i] != sli[w]:
                k = "RGB".replace(sli[i] , "").replace(sli[w] , "")
                if k == "R":
                    ans += Rcount - bisect.bisect_right(Rli , w)
                elif k == "G":
                    ans += Gcount - bisect.bisect_right(Gli , w)
                else:
                    ans += Bcount - bisect.bisect_right(Bli , w)
                if w + w - i < n and sli[w + (w - i)] == k:
                    ans -= 1

    return ans

def main():
    n = int(input())
    sli = list(str(input()))
    print(d162(n , sli))
if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
S=input()
count=0
nocount=0
Rcount=0
Gcount=0
Bcount=0
for k in range(N):
    if S[k]=="R":
        Rcount+=1
    if S[k]=="G":
        Gcount+=1
Bcount = N -Rcount-Gcount
count=Rcount*Gcount*Bcount
for k in range(3,N+1):
    if k%2==1:
        for i in range(1,k-1,2):
            if S[i-1]!=S[k-1]:
                j=(i+k)//2
                if S[i-1]!=S[j-1] and S[k-1]!=S[j-1]:
                    nocount+=1
    if k%2==0:
        for i in range(2,k-1,2):
            if S[i-1]!=S[k-1]:
                j=(i+k)//2
                if S[i-1]!=S[j-1] and S[k-1]!=S[j-1]:
                    nocount+=1
print(count-nocount)