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=input()
LEN=len(S)
END='EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE'
S += END

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


cnt=0
for j in range(1, int((LEN-1)/2)+1):
    for i in range(LEN-j*2):
        if S[i+j+j]=='E':
            break
        if (S[i] != S[i+j]) and (S[i+j] != S[i+j+j]) and (S[i] != S[i+j+j]):
            cnt+=1

print(R*G*B-cnt)

Solution (Python):
N = int(input())
S = list(input())
S_l = len(S)
ans = 0
from collections import deque
R = deque([])
G = deque([])
B  = deque([])
for i,s in enumerate(S):
    if s=='R':
        R.append(i)
    elif s=='G':
        G.append(i)
    elif s=='B':
        B.append(i)

def res(X,Y,S,i,a,b):
  x = X.copy()
  ret = 0
  for y in Y:
    if len(x)!=0:
      while x[0]<y:
        x.popleft()
        if len(x)==0:
          break
    ret+=len(x)
    tmp = 2*y-i
    if tmp>=S_l:
      continue
    if S[tmp]==a:
      ret-=1
  
  y = Y.copy()
  for x in X:
    if len(y)!=0:
      while y[0]<x:
        y.popleft()
        if len(y)==0:
          break
    ret+=len(y)
    tmp = 2*x-i
    if tmp>=S_l:
      continue
    if S[tmp]==b:
      ret-=1

      
  return ret
    
for i in range(len(S)):
    if S[i]=='R':
      r = R.popleft()
      ans += res(G,B,S,i,'G','B')
    elif S[i]=='G':
      g = G.popleft()
      ans += res(R,B,S,i,'R','B')
    else:
      b = B.popleft()
      ans += res(R,G,S,i,'R','G')
      
print(ans)

Solution (Python):
def main():
    N = int(input())
    S = input()
    R, G, B = set(), set(), set()
    for i, s in enumerate(S):
        if s == 'R':
            R.add(i)
        elif s == 'G':
            G.add(i)
        else:
            B.add(i)
    lr, lg, lb = len(R), len(G), len(B)
    
    
    
    
    
    

    a = 0

    for g in G:
        for b in B:
            b0, g0 = b, g
            if b0 < g0:
                b0, g0 = g0, b0
            r = lr
            gb = b0 - g0
            if (g0 - gb) in R:
                r -= 1
            if (b0 + gb) in R:
                r -= 1
            if gb % 2 == 0 and (g0 + gb // 2) in R:
                r -= 1
            a += r

    return a

print(main())

Solution (Python):
n = int(input())
s = input()
c_list = [(0, 0, 0) for i in range(n)]
r = 0
g = 0
b = 0
n_list = [0]*n
p = -1
for i in range(n):
  if s[i] == "R":
    r += 1
    p = 0
  elif s[i] == "G":
    g += 1
    p = 1
  else:
    b += 1
    p = 2
  c_list[i] = (r, g, b)
  n_list[i] = p
count = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c1 = n_list[i]
    c2 = n_list[j]
    if c1 != c2:
      if j+j-i < n and 3 - n_list[j+j-i] - c1 - c2 == 0:
        count += c_list[n-1][3-c1-c2] - c_list[j][3-c1-c2] -1
      else:
        count += c_list[n-1][3-c1-c2] - c_list[j][3-c1-c2]
print(count)

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

r = [0] * n
g = [0] * n
b = [0] * n
ri = gi = bi = 0
for i, c in enumerate(s):
  if c == "R":
    ri += 1
  elif c == "G":
    gi += 1
  if c == "B":
    bi += 1
  r[i] = ri
  g[i] = gi
  b[i] = bi

 
ans = r[-1] * g[-1] * b[-1]

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

print(ans)

Solution (Python):

N=int(input())
S=input()
order=list('RGB')
cnt=[[0,0,0] for _ in range(N)]
for i in range(N):
    cnt[i][order.index(S[i])]+=1
    if i!=0:
        for j in range(3):
            cnt[i][j]+=cnt[i-1][j]
ans=0
for j in range(1,N):
    for k in range(j+1,N):
        
        if S[j]==S[k]:
            continue
        else:
            s=list(set(order)-set([S[j],S[k]]))[0]
            ans+=cnt[j-1][order.index(s)]
            
            i=2*j-k
            if i>=0 and S[i]==s:
                
                ans-=1
print(ans)

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

count_R = [0]
count_G = [0]
count_B = [0]
d = {'R': count_R, 'G': count_G, 'B': count_B}
for c in s:
    count_R.append(count_R[-1]+1) if c == 'R' else count_R.append(count_R[-1])
    count_G.append(count_G[-1]+1) if c == 'G' else count_G.append(count_G[-1])
    count_B.append(count_B[-1]+1) if c == 'B' else count_B.append(count_B[-1])

res = 0
for i in range(n-2):
    for k in range(i+1, n-1):
        if s[i] == s[k]:
            continue
        st = {'R', 'G', 'B'}
        st.discard(s[i])
        st.discard(s[k])
        c = st.pop()
        cnt = d[c]
        res += cnt[-1] - cnt[k]
        if k+k-i < n and s[k+k-i] == c:
            res -= 1

print(res)

Solution (Python):
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
s = ins()
r = g = b = rg = rb = gr = gb = bg = br = 0
rgb = rbg = gbr = grb = brg = bgr = 0
rgb0 = rbg0 = gbr0 = grb0 = brg0 = bgr0 = 0

for i in range(n):
    c = s[i]
    if c=='R':
        gbr += gb
        bgr += bg
        gr += g
        br += b
        r += 1
        for j in range(1,i//2+1):
            if s[i-j]=='G' and s[i-2*j]=='B':
                bgr0 += 1
            if s[i-j]=='B' and s[i-2*j]=='G':
                gbr0 += 1
    elif c=='G':
        rbg += rb
        brg += br
        bg += b
        rg += r
        g += 1
        for j in range(1,i//2+1):
            if s[i-j]=='R' and s[i-2*j]=='B':
                brg0 += 1
            if s[i-j]=='B' and s[i-2*j]=='R':
                rbg0 += 1
    else:  
        rgb += rg
        grb += gr
        gb += g
        rb += r
        b += 1
        for j in range(1,i//2+1):
            if s[i-j]=='R' and s[i-2*j]=='G':
                grb0 += 1
            if s[i-j]=='G' and s[i-2*j]=='R':
                rgb0 += 1
print(rgb+rbg+bgr+brg+grb+gbr-(rgb0+rbg0+bgr0+brg0+grb0+gbr0))

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

total = 0

R = list()
G = list()
B = list()

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


R_len = len(R)
G_len = len(G)
B_len = len(B)

counter1 = 0
counter2 = 0
if max(R_len, G_len, B_len) == B_len:
    for i in range(R_len):
        for j in range(G_len):
            illegal = 0
            r = R[i]
            g = G[j]

            distance = abs(r - g)
            
            if r < g:
                check1 = r - distance
                check2 = g + distance
            else:
                check1 = g - distance
                check2 = r + distance

            if check1 >= 0 and S[check1] == "B":
                illegal += 1
            if check2 < N and S[check2] == "B":
                illegal += 1
            if distance % 2 == 0 and S[(r+g)//2] == "B":
                illegal += 1
   
            total += (B_len - illegal)
elif max(R_len, G_len, B_len) == G_len:
    for i in range(R_len):
        for j in range(B_len):
            illegal = 0
            r = R[i]
            b = B[j]

            distance = abs(r - b)
            
            if r < b:
                check1 = r - distance
                check2 = b + distance
            else:
                check1 = b - distance
                check2 = r + distance

            if check1 >= 0 and S[check1] == "G":
                illegal += 1
            if check2 < N and S[check2] == "G":
                illegal += 1
            if distance % 2 == 0 and S[(r+b)//2] == "G":
                illegal += 1
                
            total += (G_len - illegal)
else:
    for i in range(B_len):
        for j in range(G_len):
            illegal = 0
            b = B[i]
            g = G[j]

            distance = abs(b - g)
            
            if b < g:
                check1 = b - distance
                check2 = g + distance
            else:
                check1 = g - distance
                check2 = b + distance

            if check1 >= 0 and S[check1] == "R":
                illegal += 1
            if check2 < N and S[check2] == "R":
                illegal += 1
            if distance % 2 == 0 and S[(b+g)//2] == "R":
                illegal += 1
                
            total += (R_len - illegal)

print(total)

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

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

for i in range(len(S)):
    if S[i] == 'R':
        Rs.append(i)
    if S[i] == 'G':
        Gs.append(i)
    if S[i] == 'B':
        Bs.append(i)





sum = 0

for r in Rs:
    for g in Gs:
        sum += len(Bs)

        if r > g:
            l = r-g
            if r+l < len(S) and S[r + l] == 'B':
                sum -= 1
            if g-l > 0 and S[g - l] == 'B':
                sum -= 1

        else:
            l = g-r
            if len(S) > g + l and S[g + l] == 'B':
                sum -= 1
            if r - l > 0 and S[r - l] == 'B':
                sum -= 1

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

print(sum)

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

ans = 0

R = []
G = []
B = [0 for _ in range(N+1)]
B_cnt = 0

for i in range(1, N+1):
    if S[i-1] == "R":
        R.append(i)

    if S[i-1] == "G":
        G.append(i)

    if S[i-1] == "B":
        B[i] = 1
        B_cnt += 1

for rr in R:
    for g in G:
        l = min(rr, g)
        r = max(rr, g)
        d = r - l
        ans += B_cnt
        if d + r <= N and B[d+r] == 1:
            ans -= 1
        if d % 2 == 0 and B[l + d//2] == 1:
            ans -= 1
        if B[l - d] == 1 and 1 <= l - d:
            ans -= 1


print(ans)

Solution (Python):
n=int(input())
s=input()
c=s.count
r,g,b=c('R'),c('G'),c('B')
t=0
for i in range(n-2):
  for w in range(1,(n-i+1)//2):
    if s[i]+s[i+w]+s[i+w*2] in ('RGB','RBG','GRB','GBR','BRG','BGR'):
        t+=1
print(r*g*b-t)

Solution (Python):
N=int(input())
S=input()
Rd=set();Gd=set();Bd=set()
for i in range(0,N):
    if S[i]=='R':
        Rd.add(i)
    elif S[i]=='G':
        Gd.add(i)
    elif S[i]=='B':
        Bd.add(i)
RN=len(Rd);GN=len(Gd);BN=len(Bd)
s=0
for r in Rd:
    for g in Gd:
        if g>r:
            if 2*r-g in Bd:
                s+=1
                
            if (r+g)%2==0 and (r+g)//2 in Bd:
                s+=1
                
            if 2*g-r in Bd:
                s+=1
                
        else:
            if 2*r-g in Bd:
                s+=1
                
            if 2*g-r in Bd:
                s+=1
                
            if (r+g)%2==0 and (r+g)//2 in Bd:
                s+=1
                
print(RN*GN*BN-s)

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

Solution (Python):
import itertools

n = int(input())
s = input().rstrip()

colname = {'R': 0, 'G': 1, 'B': 2}
color = [[], [], []]
for i in range(n):
    ci = colname[s[i]]
    color[ci].append(i + 1)
color = sorted(color, key=lambda x: len(x))

color[2] = set(color[2])
nc2 = len(color[2])

x = 0
for r in color[0]:
    for g in color[1]:
        x += nc2
        if r > g:
            if (r + (r - g)) in color[2]:
                x -= 1
            if (g - (r - g)) in color[2]:
                x -= 1
        else:
            if (g + (g - r)) in color[2]:
                x -= 1
            if (r - (g - r)) in color[2]:
                x -= 1
        c = r + g
        if c % 2 == 0 and (c // 2) in color[2]:
            x -= 1
print(x)

Solution (Python):
N = int(input())
l = list(input())
from collections import deque
r = deque()
g = []
b = deque()
for i in range(N):
    if l[i] == "R":
        r.append(i)
    elif l[i] == "G":
        g.append(i)
    else:
        b.append(i)
from collections import defaultdict
d = defaultdict(int)
for i in range(len(b)):
    d[b[i]] = 1
ans = 0
na = len(b)
while r:
    rr = r.pop()
    for i in range(len(g)):
        gg = g[i]
        ans += na - d[(rr+gg) / 2] - d[min(rr,gg)-abs(gg-rr)] - d[max(rr,gg)+abs(gg-rr)]
print(ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
import bisect as bs

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

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

    ans = 0
    for i in R:
        index_G = bs.bisect_left(G, i)
        index_B = bs.bisect_left(B, i)
        
        ans += index_G*(len(B)-index_B)
        
        for j in range(index_G):
            if 2*i-G[j]<N and S[2*i-G[j]]=='B':
                ans -= 1
        ans += index_B*(len(G)-index_G)
        for j in range(index_B):
            if 2*i-B[j]<N and S[2*i-B[j]]=='G':
                ans -= 1
    
    for i in G:
        index_R = bs.bisect_left(R, i)
        index_B = bs.bisect_left(B, i)
        
        ans += index_R*(len(B)-index_B)
        for j in range(index_R):
            if 2*i-R[j]<N and S[2*i-R[j]]=='B':
                ans -= 1
        
        ans += index_B*(len(R)-index_R)
        for j in range(index_B):
            if 2*i-B[j]<N and S[2*i-B[j]]=='R':
                ans -= 1
        
    
    for i in B:
        index_G = bs.bisect_left(G, i)
        index_R = bs.bisect_left(R, i)
        
        ans += index_G*(len(R)-index_R)
        for j in range(index_G):
            if 2*i-G[j]<N and S[2*i-G[j]]=='R':
                ans -= 1
        ans += index_R*(len(G)-index_G)
        for j in range(index_R):
            if 2*i-R[j]<N and S[2*i-R[j]]=='G':
                ans -= 1
    

    print(ans)


if __name__ == '__main__':
    resolve()

Solution (Python):
import collections
n = int(input())
s = list(input())
a = collections.Counter(s)
a = list(a.items())
ans = 0





















if n == 1 or n == 2 or n == 3:
    print(0)
    exit()
if len(a) != 3:
    print(0)
    exit()
for i in range(1,n//2+3):
    cnt = 0
    
    for j in range(n):
        if cnt + 2*i > n-1:
            break
        x,y,z = s[cnt], s[cnt+i], s[cnt+2*i]
        
        if x != y and y != z and z != x:
            ans += 1
        cnt += 1
print(a[0][1]*a[1][1]*a[2][1]-ans)

Solution (Python):
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]


def comb(n,r):
    if r==2:
        return n*(n-1)//2
    else:
        return n*(n-1)*(n-2)//6
n=n0()
s=s0()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=0
for i in range(1,(n-1)//2+1):
    for j in range(0,n-2*i):
        ans+=1
        if s[j]==s[j+i]==s[j+2*i]:
            ans-=1
        elif s[j]==s[j+i] or s[j]==s[j+2*i] or s[j+i]==s[j+2*i]:
            ans-=1
            
ans+=comb(r,3)+comb(g,3)+comb(b,3)+comb(r,2)*(g+b)+comb(g,2)*(r+b)+comb(b,2)*(r+g)
print(comb(n,3)-ans)

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)

cnt=len(R)*len(G)*len(B)
for i in R:
  for j in G:
    if (i+j)%2==0 and S[(i+j)//2]=='B':
      cnt-=1
for i in R:
  for j in B:
    if (i+j)%2==0 and S[(i+j)//2]=='G':
      cnt-=1
for i in B:
  for j in G:
    if (i+j)%2==0 and S[(i+j)//2]=='R':
      cnt-=1      
print(cnt)

Solution (Python):
def main():
    N = int(input())
    S = input()
    rcnt, gcnt, bcnt = 0, 0, 0
    for i, s in enumerate(S):
        if s=='R':
            rcnt += 1
        if s=='G':
            gcnt += 1
        if s=='B':
            bcnt += 1
    
    base = rcnt*gcnt*bcnt
    interval = 1
    totalminus = 0
    limit = 1 + (N-3)//2
    while interval <= limit:
        for i in range(interval, N-interval):
            a = S[i-interval]
            b = S[i]
            c = S[i+interval]
            if a!=b and b!=c and c!=a:
                totalminus += 1
        interval += 1
    print(base-totalminus)
main()

Solution (Python):
n = int(input())
s = input()
 
num_rgb = {'R':0, 'G':0, 'B':0}
num_list = []
for i in range(n):
    num_rgb[s[i]] += 1
    num_list.append(num_rgb.copy())
 
total = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i] == s[j]:
            continue
        target = ''
        if not (s[i] == 'R' or s[j] == 'R'):
            target = 'R'
        elif not (s[i] == 'G' or s[j] == 'G'):
            target = 'G'
        elif not (s[i] == 'B' or s[j] == 'B'):
            target = 'B'
        num = num_list[n-1][target]-num_list[j][target]
        if 2*j-i < n and s[j+j-i] == target:
            num -= 1
        total += num
print(total)

Solution (Python):



N = int(input())
A = input()

R = [0 for _ in range(N + 5)]
G = [0 for _ in range(N + 5)]
B = [0 for _ in range(N + 5)]

for i in range(N):
    if(i != 0):
        R[i] = R[i - 1]
        G[i] = G[i - 1]
        B[i] = B[i - 1]
    if (A[i] == 'R'):
        R[i] += 1
    elif (A[i] == 'G'):
        G[i] += 1
    elif (A[i] == 'B'):
        B[i] += 1
        
ans = 0
for i in range(1, N):
    if A[i] == 'R':
       ans += G[i - 1] * (B[N - 1] - B[i]) 
       ans += B[i - 1] * (G[N - 1] - G[i]) 
    if A[i] == 'G':
       ans += R[i - 1] * (B[N - 1] - B[i]) 
       ans += B[i - 1] * (R[N - 1] - R[i]) 
    if A[i] == 'B':
       ans += G[i - 1] * (R[N - 1] - R[i]) 
       ans += R[i - 1] * (G[N - 1] - G[i])
    
    for j in range(1, min([N - i - 1, i]) + 1):
        if A[i - j] != A[i] and A[i] != A[i + j] and A[i - j] != A[i + j]:
            ans -= 1
    

print(ans)

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

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

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

print(r * g * b - count)

Solution (Python):
N = int(input())
S = tuple([s for s in input()])
B = {'R','G','B'}
ans = 0
d = {}
d['R'] = 0
d['G'] = 1
d['B'] = 2
num = [[0,0,0]for _ in range(N+1)]
for i in range(N):
    for j in range(3):
        num[i+1][j] = num[i][j]
        if d[S[i]] == j:
            num[i+1][j] += 1
num = tuple(num[1:])
for i in range(N-2):
    s = S[i]
    for j in range(i+1,N-1):
        if S[j] != s:
            l = j-i
            t = (B-{s,S[j]}).pop()
            L = 0
            R = 0
            if l > N-j-1:
                ans += num[N-1][d[t]]-num[j][d[t]]
            else:
                L = num[j+l-1][d[t]]-num[j][d[t]]
                R = num[N-1][d[t]]-num[j+l][d[t]]
                ans += L+R
print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N = int(input())
    S = input()
    sub_sum = {key : [0]*N for key in "RGB"}
    
    for i, key in enumerate(S):
        sub_sum[key][i] = 1
    
    for i in range(1, N):
        for key in "RGB":
            sub_sum[key][i] += sub_sum[key][i-1]

    ans = 0
    for i in range(1, N):
        now = S[i]
        right_nums = {key:sub_sum[key][N-1]-sub_sum[key][i-1] for key in "RGB"}
        for j in range(i):
            left = S[j]
            right = [c for c in "RGB" if  c not in [now, left]][0]
            right_num = right_nums[right]
            if now != left:
                if 2*i - j < N and right_num:
                    if S[2*i - j] == right:
                        right_num -= 1
                ans += right_num
    print(ans)




if __name__ == "__main__":
    main()

Solution (Python):
import bisect

n = int(input())
s = input()
r = []
b = []
g = []
for i,c in enumerate(s):
    if c == 'R':
        r.append(i)
    elif c == 'B':
        b.append(i)
    else:
        g.append(i)
g_leng = len(g)
r_leng = len(r)
b_leng = len(b)
ans = g_leng*r_leng*b_leng
subans = 0
for j in b:
    for i in r:
        g_ind = bisect.bisect_left(g, 2*j-i)
        if g_ind != g_leng and g[g_ind] == 2*j-i:
            subans+=1
        g_ind = bisect.bisect_left(g, 2*i-j)
        if g_ind != g_leng and g[g_ind] == 2*i-j:
            subans+=1
        g_ind = bisect.bisect_left(g, (i + j) // 2)
        if g_ind != g_leng and g[g_ind] == (i + j) / 2:
            subans += 1
print(ans-subans)

Solution (Python):
import bisect
n = int(input())
S = list(input())
R, G, B = [], [], []
for i in range(n):
    if S[i] == 'R':
        R.append(i)
    if S[i] == 'G':
        G.append(i)
    if S[i] == 'B':
        B.append(i)
r = len(R)
g = len(G)
b = len(B)
cnt = 0
Rset, Gset, Bset = set(R), set(G), set(B)

for i in range(r):
    g0 = bisect.bisect_right(G, R[i])
    for j in range(g0, g):
        b0 = bisect.bisect_right(B, G[j])
        cnt += b - b0
        x = 2*G[j]-R[i]
        if x in Bset:
            cnt -= 1

for i in range(r):
    b0 = bisect.bisect_right(B, R[i])
    for j in range(b0, b):
        g0 = bisect.bisect_right(G, B[j])
        cnt += g - g0
        x = 2*B[j]-R[i]
        if x in Gset:
            cnt -= 1

for i in range(g):
    b0 = bisect.bisect_right(B, G[i])
    for j in range(b0, b):
        r0 = bisect.bisect_right(R, B[j])
        cnt += r - r0
        x = 2*B[j]-G[i]
        if x in Rset:
            cnt -= 1

for i in range(g):
    r0 = bisect.bisect_right(R, G[i])
    for j in range(r0, r):
        b0 = bisect.bisect_right(B, R[j])
        cnt += b - b0
        x = 2*R[j]-G[i]
        if x in Bset:
            cnt -= 1

for i in range(b):
    r0 = bisect.bisect_right(R, B[i])
    for j in range(r0, r):
        g0 = bisect.bisect_right(G, R[j])
        cnt += g - g0
        x = 2*R[j]-B[i]
        if x in Gset:
            cnt -= 1

for i in range(b):
    g0 = bisect.bisect_right(G, B[i])
    for j in range(g0, g):
        r0 = bisect.bisect_right(R, G[j])
        cnt += r - r0
        x = 2*G[j]-B[i]
        if x in Rset:
            cnt -= 1
print(cnt)

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

from collections import Counter

m = Counter(S)

ans = m['R']*m['G']*m['B']
non = 0


for i in range ((N-1)//2):
    st = 0
    while st + (i+1)*2 < N:
        if S[st] != S[st+i+1] and S[st+i+1] != S[st+2*(i+1)] and S[st] != S[st+2*(i+1)]:
            non += 1
        st += 1
        
print(ans-non)

Solution (Python):
def main():
    import math
    import sys
    from collections import defaultdict

    n = int(input())
    s = str(input())
    dic1 = defaultdict(list)

    for i in range(len(s)):
        dic1[s[i]] += [i]

    c = 0
    co = len(dic1["B"])

    for a in dic1["R"]:
        for b in dic1["G"]:
            f = 0
            t = abs(a-b)
            ma = max(a,b) + t
            mi = min(a,b) - t
            if abs(a + b) % 2 == 0:
                if s[abs(a + b) // 2] == "B":
                    f += 1
            if ma <= len(s) - 1:
                if s[ma] =="B":
                    f += 1
            if mi >= 0:
                if s[mi] == "B":
                    f += 1
            c += (co - f)

    print(c)



if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

if __name__ == '__main__':
    N = int(input())
    S = input()

    cntR_from_r = [0 for _ in range(N)]
    cntG_from_r = [0 for _ in range(N)]
    cntB_from_r = [0 for _ in range(N)]

    for i in range(N - 1, 0, -1):
        if S[i] == 'R':
            cntR_from_r[i - 1] = cntR_from_r[i] + 1
            cntG_from_r[i - 1] = cntG_from_r[i]
            cntB_from_r[i - 1] = cntB_from_r[i]
        elif S[i] == 'G':
            cntR_from_r[i - 1] = cntR_from_r[i]
            cntG_from_r[i - 1] = cntG_from_r[i] + 1
            cntB_from_r[i - 1] = cntB_from_r[i]
        else:
            cntR_from_r[i - 1] = cntR_from_r[i]
            cntG_from_r[i - 1] = cntG_from_r[i]
            cntB_from_r[i - 1] = cntB_from_r[i] + 1

    ans = 0
    for i in range(N):
        if S[i] == 'R':
            val1 = cntG_from_r[i] * cntB_from_r[i]
            for j in range(1, N):
                j1 = i - j
                j2 = i + j

                if j1 < 0 or j2 > N - 1:
                    break
                else:
                    if (S[j1] == 'G' and S[j2] == 'B') or (S[j1] == 'B' and S[j2] == 'G'):
                        val1 -= 1
            ans += val1
        elif S[i] == 'G':
            val1 = cntR_from_r[i] * cntB_from_r[i]
            for j in range(1, N):
                j1 = i - j
                j2 = i + j
                if j1 < 0 or j2 > N - 1:
                    break
                else:
                    if (S[j1] == 'R' and S[j2] == 'B') or (S[j1] == 'B' and S[j2] == 'R'):
                        val1 -= 1
            ans += val1
        else:
            val1 = cntG_from_r[i] * cntR_from_r[i]
            for j in range(1, N):
                j1 = i - j
                j2 = i + j
                if j1 < 0 or j2 > N - 1:
                    break
                else:
                    if (S[j1] == 'R' and S[j2] == 'G') or (S[j1] == 'G' and S[j2] == 'R'):
                        val1 -= 1
            ans += val1
    print(ans)

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

rc = S.count("R")
gc = S.count("G")

tc = int(rc*gc*(N-rc-gc))

aa = 0
for i in range(N):
  for j in range(1,math.ceil((N-i)/2)+1):
    
    if i+j+j < N:
      if S[i] != S[i+j]:
        if S[i] != S[i+j+j]:
          if S[i+j] != S[i+j+j]:
            aa = int(aa + 1)
            
print(int(tc-aa))

Solution (Python):
def main():
    n = int(input())
    s = list(input())
    ss = list(sorted(list(set(s))))
    s_sorted = list(sorted(s))
    if len(ss) < 3:
        print(0)
        return
    m = n-1
    sn = [0]*3
    j = 0
    flag = False
    for i, si in enumerate(ss):
        while s_sorted[j] == si:
            sn[i] += 1
            if j == m:
                flag = True
                break
            j += 1
        if flag:
            break
    ans = sn[0]*sn[1]*sn[2]
    for d in range(1, n//2+1):
        i = 0
        d2 = 2*d
        ma = n-d2
        while i < ma:
            si, sj, sk = s[i], s[i+d], s[i+d2]
            if si != sj and sj != sk and si != sk:
                ans -= 1
            i += 1
    print(ans)
                

if __name__ == "__main__":
    main()

Solution (Python):
import math
N = int(input())
S = input()
r = 0
g = 0
b = 0
T = list()
for i in range(N):
    if(S[i] == "R"):
        r += 1
        T.append(1)
    elif(S[i] == "G"):
        g += 1
        T.append(2)
    elif(S[i] == "B"):
        b += 1
        T.append(3)
total = r*g*b
for j in range(1, N-1):
    for n in range(1, min(N-1-j,j)+1):
        if (T[j]*T[j-n]*T[j+n] == 6):
            total -= 1
print(total)

Solution (Python):
import sys
from collections import deque
input = lambda: sys.stdin.readline().rstrip()
aint = lambda: int(input())

n = aint()
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')
a = 0
if n<4:
    print(0)
else:
    for i in range(len(s)-2):
        for j in range(int(0.5*(len(s)-i-1))):
            if s[i]!=s[i+j+1] and s[i+j+1]!=s[i+2*j+2] and s[i]!=s[i+2*j+2]:
                a += 1
    print(r*g*b-a)

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

def linput():
    return [int(x) for x in input().split()]

n = int(input())
s = input().rstrip()

from collections import Counter

r = [0] * (n + 1)
g = [0] * (n + 1)
b = [0] * (n + 1)



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

ans = 0
for i in range(n):
    for j in range(i + 1, n - 1):
        if s[i] == s[j]:
            continue
        ng = j + (j - i) + 1
        if s[i] == "R" and s[j] == "G":
            l = b
        elif s[i] == "R" and s[j] == "B":
            l = g
            
        elif s[i] == "G" and s[j] == "R":
            l = b
            
        elif s[i] == "G" and s[j] == "B":
            l = r
            
        elif s[i] == "B" and s[j] == "R":
            l = g
            
        elif s[i] == "B" and s[j] == "G":
            l = r
            
        ans += l[n] - l[j + 1]
        if ng < n + 1:
            ans -= l[ng] - l[ng - 1]
        

print(ans)

Solution (Python):


n = 0
s = ""

def format_input(filename = None):
	global n
	global s
	if filename == None:
		n = int(input())
		s = str(input())

	elif filename == '__random__':
		from random import randint as rng
		from random import choices
		n = rng(1, 4 * 10**3)
		s = ''.join(choices('RGB', k = n))
		print(n)
		print(s)

def get_answer():
	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)

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

	for l in r:
		for m in g:
			if (l + m) % 2 == 0:
				if s[(l + m) // 2] == 'B':
					answer -= 1
			if max(l, m) + abs(l - m) < n:
				if s[max(l, m) + abs(l - m)] == 'B':
					answer -= 1
			if min(l, m) - abs(l - m) >= 0:
				if s[min(l, m) - abs(l - m)] == 'B':
					answer -= 1
	return answer

if __name__ == '__main__':
	format_input()

	ans = get_answer()
	print(ans)

Solution (Python):
def resolve():
    n = int(input())
    s = input()
    rlist = []
    glist = []
    blist = []
    for i in range(n):
        if s[i] == "R":
            rlist.append(i)
        elif s[i] == "G":
            glist.append(i)
        else:
            blist.append(i)
    blist = set(blist)
    ans = 0
    for i in range(len(rlist)):

        for j in range(len(glist)):
            r = rlist[i]
            ng = 0
            g = glist[j]
            s = sorted([r, g])
            r = s[0]
            g = s[1]
            if (r-(g-r)) in blist:
                ng += 1
            if (g+(g-r)) in blist:
                ng += 1
            if (r+g) % 2 == 0 and (r+g)//2 in blist:
                ng += 1
            ans += len(blist)-ng
    print(ans)
resolve()