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 = tuple(input())
ans = 0
R = S.count('R')
G = S.count('G')
B = S.count('B')
for i in range(n):
    a = S[i]
    for j in range(i):
        b = S[j]
        if a == b:
            continue
        else:
            for d in ['R','G','B']:
                if d ==a or d == b:
                    continue
                else:
                    c = d
            if c == 'R':
                ans+=R
            elif c =='G':
                ans+=G
            else:
                ans+=B
            if (2*j-i)<0 :
                pass
            elif S[2*j-i] == c :
                ans -=1
            if (2 * i - j) >= n:
                pass
            elif S[2*i-j] ==c:
                ans -=1
            if (i+j)%2 == 0:
                if S[(i+j)//2] == c:
                    ans -=1
ans /=3
print(int(ans))

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

d = {'R':[],'G':[],'B':[]}
for i,c in enumerate(S):
    d[c].append(i)
ld = set(d['B'])

ans = 0
for si in d['R']:
    for sj in d['G']:
        tmp = len(ld)
        if max(si,sj) + abs(si-sj) in ld:
            tmp -= 1
        if min(si,sj) - abs(si-sj) in ld:
            tmp -= 1
        if min(si,sj) + abs(si-sj)/2 in ld:
            tmp -= 1
        ans += tmp

print(ans)

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

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

ans=len(r)*len(g)*len(b)
for gap in range(1,(n-1)//2+1):
    i=0
    j=i+gap
    k=j+gap
    while k<=n-1:
        if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
            ans-=1
        i+=1
        j+=1
        k+=1
    
print(ans)

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

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

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

Solution (Python):
import sys
from math import sqrt
from collections import Counter, defaultdict, deque

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)


def I():
    return int(input())


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


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    n = I()
    s = input().rstrip()

    r = [i for i, x in enumerate(s) if x == "R"]
    b = [i for i, x in enumerate(s) if x == "B"]
    g = [i for i, x in enumerate(s) if x == "G"]

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

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


if __name__ == "__main__":
    main()

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

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

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

print(R*G*B-e)

Solution (Python):
from sys import stdin
import heapq
def main():

    input = stdin.readline

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

    R_num = S.count("R")
    G_num = S.count("G")
    B_num = S.count("B")

    ans_sum = R_num*G_num*B_num

    for i in range(N):
        for j in range(1,((N-i-1)//2)+1):
            if (S[i] != S[i+j]) & (S[i+j] != S[i+j*2]) & (S[i] != S[i+j*2]):
                ans_sum -= 1

    print(ans_sum)
 
if __name__ == "__main__":
    main()

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

if n == 1:
    print(0)
    exit(0)

r_count = 0
g_count = 0
b_count = 0
for idx, c in enumerate(s):
    if c == 'R':
        r_count += 1
    elif c == 'G':
        g_count += 1
    elif c == 'B':
        b_count += 1


i = 0
removed = 0
while True:
    i += 1
    if i > n // 2:
        break
    for j in range(n):
        if j + i * 2 >= n:
            break
        if s[j] != s[j + i] and s[j + i] != s[j + i * 2] and s[j] != s[j + i * 2]:
            
            removed += 1
    

print(r_count * g_count * b_count - removed)

Solution (Python):

n = int(input())
s = list(input())
cr = [0] * n
cg = [0] * n
cb = [0] * n
for i in range(n):
    if i == 0:
        if s[i] == 'R':
            cr[i] += 1
        elif s[i] == 'G':
            cg[i] += 1
        else:
            cb[i] += 1
    else:
        cr[i] = cr[i-1]
        cg[i] = cg[i-1]
        cb[i] = cb[i-1]
        if s[i] == 'R':
            cr[i] += 1
        elif s[i] == 'G':
            cg[i] += 1
        else:
            cb[i] += 1
ans = 0
for i in range(n-2):
    a = s[i]
    for j in range(i+1, n-1):
        b = s[j]
        if a == b:
            continue
        diff = j - i
        c = list({'R', 'G', 'B'} - {a, b})[0]
        if c == 'R':
            ans += cr[-1] - cr[j]
        elif c == 'G':
            ans += cg[-1] - cg[j]
        else:
            ans += cb[-1] - cb[j]
        if j + diff < n:
            if s[j + diff] == c:
                ans -= 1
print(ans)

Solution (Python):
from itertools import product as pro

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']
cnt = len(r)*len(g)*len(b)

length = max(len(r), len(g), len(b))
if length == len(r):
    max1 = set(r)
    min1 = g
    min2 = b

if length == len(g):
    max1 = set(g)
    min1 = r
    min2 = b

if length == len(b):
    max1 = set(b)
    min1 = g
    min2 = r

for i, j in pro(min1, min2):
    if 2*j-i in max1:
        cnt -= 1
    if 2*i-j in max1:
        cnt -= 1
    if (i+j)/2 in max1:
        cnt -= 1

print(cnt)

Solution (Python):
N = int(input())
S = input()
T = [0]*3

for i in range(N):
    if S[i] == "R":
        T[0] += 1
    elif S[i] == "G":
        T[1] += 1
    else:
        T[2] += 1
ans = T[0]*T[1]*T[2]
t = N // 2

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

print(ans)

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

all = s.count('R') * s.count('G') * s.count('B')
max_diff = (n - 3) // 2
for i in range(max_diff + 1):
    for j in range(n - i - i - 2):
        if s[j] != s[j + 1 + i] and s[j] != s[j + 1 + i + 1 + i] and s[j + 1 + i] != s[j + 1 + i + 1 + i]:
            all -= 1
print(all)

Solution (Python):
import math
N=int(input())
S=input()
ans=0
R=[]
G=[]
B=[]
for i,s in enumerate(S):
  if s=="R":
    R.append(i)
  elif s=="G":
    G.append(i)
  else:
    B.append(i)
ans=len(R)*len(G)*len(B)

for r in R:
  for g in G:
    if (r+g)%2==0:
      if S[(r+g)//2]=="B":
        ans-=1
    
for r in R:
  for b in B:
    if(r+b)%2==0:
      if S[(r+b)//2]=="G":
        ans-=1
    
for g in G:
  for b in B:
    if (g+b)%2==0:
      if S[(g+b)//2]=="R":
        ans-=1
    
print(ans)

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

    ans = 0
    ruiseki_r = [0]*N
    ruiseki_g = [0]*N
    ruiseki_b = [0]*N

    for i in range(N):
        if S[i] == "R":
            ruiseki_r[i] = ruiseki_r[i-1] + 1
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "G":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1] + 1
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "B":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1] + 1
    set_rg = set(["R", "G"])
    set_gb = set(["G", "B"])
    set_rb = set(["R", "B"])
    for i in range(N-2):
        Si = S[i]
        for j in range(i+1, N-1):
            Sj = S[j]
            if Si != Sj:
                Sij = set([Si, Sj])
                if len(Sij^set_rg) == 0:
                    remain_b = ruiseki_b[-1] - ruiseki_b[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "B":
                            remain_b -= 1
                    ans = ans + remain_b
                if len(Sij^set_gb) == 0:
                    remain_r = ruiseki_r[-1] - ruiseki_r[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "R":
                            remain_r -= 1
                    ans = ans + remain_r
                if len(Sij^set_rb) == 0:
                    remain_g = ruiseki_g[-1] - ruiseki_g[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "G":
                            remain_g -= 1
                    ans = ans + remain_g
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
import collections
from collections import deque
from functools import reduce
import heapq
n = int(input())

s = input()


def main():
    r = []
    g = []
    b = []

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


    ans = 0
    z = len(b)
    for i in range(len(r)):
        for j in range(len(g)):
            d = abs(r[i] - g[j])
            e = 0
            p = min(r[i], g[j]) - d
            q = max(r[i], g[j]) + d
            if (r[i] + g[j]) % 2 == 0:
                t = int((r[i] + g[j]) / 2)
                if p >= 0:
                    if s[p] == "B":
                        e += 1
                if q < n:
                    if s[q] == "B":
                        e += 1
                if s[t] == "B":
                    e += 1
            else:
                if p >= 0:
                    if s[p] == "B":
                        e += 1
                if q < n:
                    if s[q] == "B":
                        e += 1

            ans += (z - e)

    print(ans)
            
main()

Solution (Python):
n=int(input())
s=input()
rr=[]
gg=[]
bb=[]
for i in range(n):
  if s[i]=='R':
    rr.append(i)
  elif s[i]=='G':
    gg.append(i)
  else:
    bb.append(i)
lr=len(rr)
lg=len(gg)
lb=len(bb)
ans=lr*lg*lb
if ans==0:
  print(0)
  exit()

for xx in rr:
  for yy in gg:
    x,y=min(xx,yy),max(xx,yy)
    d=y-x
    a=x-d
    b=y+d
    if 0<=a<n and s[a]=='B':
      ans-=1
    if 0<=b<n and s[b]=='B':
      ans-=1
    if (x+y)%2==0:
      c=(x+y)//2
      if 0<=c<n and s[c]=='B':
        ans-=1




print(ans)

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

R,G,B = [],[],[]

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

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

N2 = int(N/2 + 0.5)
for i in range(0,N):
    stri = S[i]
    for j in range(0, N2):
        if i+2*j >= N:
            break
        strj = S[i+j]
        strk = S[i+2*j]
        if stri != strj and strj != strk and stri != strk:

            count -= 1
print(count)

Solution (Python):

import sys
N=int(sys.stdin.readline().strip())
S=sys.stdin.readline().strip()
R=[]
G=[]
B=[]
for x in S:
    if x=="R":
        R.append(1)
        G.append(0)
        B.append(0)
    elif x=="G":
        R.append(0)
        G.append(1)
        B.append(0)
    elif x=="B":
        R.append(0)
        G.append(0)
        B.append(1)


RR=[0]
GG=[0]
BB=[0]
for r in R: RR.append(RR[-1]+r)
for g in G: GG.append(GG[-1]+g)
for b in B: BB.append(BB[-1]+b)

ans=0

for i in range(N):
    for j in range(i+1,N):
        if S[i]=="R" and S[j]=="G":
            ans+=BB[-1]-BB[j+1]
            try:
                if S[j+j-i]=="B":
                    ans-=1
            except:
                pass
        elif S[i]=="R" and S[j]=="B":
            ans+=GG[-1]-GG[j+1]
            try:
                if S[j+j-i]=="G":
                    ans-=1
            except:
                pass
        elif S[i]=="G" and S[j]=="B":
            ans+=RR[-1]-RR[j+1]
            try:
                if S[j+j-i]=="R":
                    ans-=1
            except:
                pass
        elif S[i]=="G" and S[j]=="R":
            ans+=BB[-1]-BB[j+1]
            try:
                if S[j+j-i]=="B":
                    ans-=1
            except:
                pass
        elif S[i]=="B" and S[j]=="G":
            ans+=RR[-1]-RR[j+1]
            try:
                if S[j+j-i]=="R":
                    ans-=1
            except:
                pass
        elif S[i]=="B" and S[j]=="R":
            ans+=GG[-1]-GG[j+1]
            try:
                if S[j+j-i]=="G":
                    ans-=1
            except:
                pass
print ans

Solution (Python):
N=int(input())
S=list(str(input()))
r=S.count('R')
g=S.count('G')
b=S.count('B')
count=0

for i in range(int((N-3)/2)+1):
    for k in range(N-i*2-3+1):
        if S[k]!=S[k+i+1] and S[k]!=S[k+2*i+2] and S[k+i+1]!=S[k+2*i+2]:
            count+=1
print(r*g*b-count)

Solution (Python):
def main():
    import sys
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    R = [0] * N
    G = [0] * N
    B = [0] * N
    rcnt = 0
    gcnt = 0
    bcnt = 0
    rgb = {
        ('R', 'G'):'B',
        ('G', 'R'):'B',
        ('R', 'B'):'G',
        ('B', 'R'):'G',
        ('G', 'B'):'R',
        ('B', 'G'):'R'
    }
    for i, s in enumerate(S):
        if s == 'R':
            rcnt += 1
        elif s == 'G':
            gcnt += 1
        elif s == 'B':
            bcnt += 1
        R[i] = rcnt
        G[i] = gcnt
        B[i] = bcnt
    
    
    
    ans = 0
    for i in range(N-1):
        for j in range(i+1, N):
            
            if S[i] == S[j]:
                
                continue
            s = rgb[S[i], S[j]]
            if s == 'R':
                ans += rcnt - R[j]
                
            elif s == 'G':
                ans += gcnt - G[j]
                
            elif s == 'B':
                ans += bcnt - B[j]
                
            tn = j + (j-i)
            if tn < N and S[tn] == s:
                ans -= 1

    print(ans)




if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

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

dd = defaultdict(int)
rList = [0]
gList = [0]
bList = [0]
for i in range(N):
    if S[i] == "R":
        rList.append(rList[-1] + 1)
        gList.append(gList[-1])
        bList.append(bList[-1])
    elif S[i] == "G":
        rList.append(rList[-1])
        gList.append(gList[-1] + 1)
        bList.append(bList[-1])
    else:
        rList.append(rList[-1])
        gList.append(gList[-1])
        bList.append(bList[-1] + 1)

ans = 0
for i in range(N):
    for j in range(i, N):
        if S[i] == S[j]:
            continue
        tl = ["R", "G", "B"]
        tl.remove(S[i])
        tl.remove(S[j])
        target = tl[0]
        if target == "R":
            ans += rList[-1] - rList[j + 1]
        elif target == "G":
            ans += gList[-1] - gList[j + 1]
        else:
            ans += bList[-1] - bList[j + 1]

        diff = abs(i - j)
        if j + diff <= N - 1 and S[j + diff] == target:
            ans -= 1

print(ans)

Solution (Python):













n = int(input())
s = str(input())
numr = s.count("R")
numg = s.count("G")
numb = s.count("B")
ans = numr*numb*numg
for i in range(len(s)):
    if s[i] == "R":
        for j in range(len(s)):
            if s[j] == "G":
                if i<j:
                    if i+2*(j-i) < n and  s[i+2*(j-i)] == "B":
                        ans -= 1
                    if i-(j-i) >= 0 and  s[i-(j-i)] ==  "B":
                        ans -= 1
                if i>j:
                    if i-2*(i-j) >= 0 and  s[i-2*(i-j)] == "B":
                        ans -= 1
                    if i+(i-j) < n and s[i+(i-j)] ==  "B":
                        ans -= 1
                if (i+j)%2 == 0 and s[(i+j)//2] == "B":
                    ans -= 1

print(ans)

Solution (Python):
num = int(input())
s = list(input())

red_index = [i for i in range(1,len(s)+1) if s[i-1] == 'R']
gre_index = [i for i in range(1,len(s)+1) if s[i-1] == 'G']
blu_index = [i for i in range(1,len(s)+1) if s[i-1] == 'B']
ans = len(red_index) * len(gre_index) * len(blu_index)

delete = 0
for i in range(1,(num // 2) + 1):
  for st in range(num):
    if st+2*i <= num -1:
      check = [s[st], s[st + i], s[st + 2*i]]
      if len(set(check)) == 3:
        delete += 1
print(ans-delete)

Solution (Python):
from sys import stdin

N = int(stdin.readline())
S = list(input())
R = set()
G = set()
B = set()
for i, s in enumerate(S, 1):
    if s == 'R':
        R.add(i)
    elif s == 'G':
        G.add(i)
    else:
        B.add(i)

b = len(B)
ans = 0
for r in R:
    for g in G:
        M = max(r, g)
        m = min(r, g)
        if 2 * m - M in B:
            ans -= 1
        if 2 * M - m in B:
            ans -= 1
        if (M + m) / 2 in B:
            ans -= 1
        ans += b

print(ans)

Solution (Python):


n = int(input())
s = input()
assert len(s) == n


post = {a: [0] * (n + 1) for a in 'RGB'}
for i in range(n - 1, -1, -1):
    for a in 'RGB':
        post[a][i] = post[a][i + 1]
    post[s[i]][i] = post[s[i]][i + 1] + 1

def remain(a, b):
    return (list(set('RGB') - {a, b}))[0]

total = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        r = remain(s[i], s[j])
        total += post[r][j + 1]
        if j - i + j < n and s[j - i + j] == r:
            total -= 1
        
print(total)

Solution (Python):
def CountComp(N,S):
    Ncomp = 0
    for i in range(N):
        for j in range(1,(N-i+1)//2):
            
            if S[i]==S[i+j] or S[i]==S[i+2*j] or S[i+j]==S[i+2*j]:
                pass
            else:
                Ncomp+=1
    return Ncomp

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


NR = S.count('R')
NG = S.count('G')
NB = S.count('B')


Nmatch = NR*NG*NB-CountComp(N,S)
print(Nmatch)

Solution (Python):
import sys

input = sys.stdin.readline

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

dic = {}
ans = 0

for i in range(N-2):
    dic['R'] = S[i+1:].count('R')
    dic['G'] = S[i+1:].count('G')
    dic['B'] = S[i+1:].count('B')
    for j in range(i+1, N-1):
        dic[S[j]] -= 1
        arr = ['R', 'G', 'B']
        if S[i] != S[j]:
            arr.remove(S[i])
            arr.remove(S[j])
            ans += dic[arr[0]]
            if j+j-i <= N-1 and S[j+j-i] == arr[0]:
                ans -= 1

print(ans)

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

rlis=[]
glis=[]
blis=[]
for i in range(len(s)):
    if s[i]=="R":
        rlis.append(i)
    if s[i]=="G":
        glis.append(i)
    if s[i]=="B":
        blis.append(i)
import bisect
ans=0

lis1=rlis
lis2=glis
lis3=blis

for j in lis1:
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)

    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp

    lis2,lis3=lis3,lis2
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)
    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp


lis1=glis
lis2=blis
lis3=rlis
for j in lis1:
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)

    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp

    lis2,lis3=lis3,lis2
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)
    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp

lis1=blis
lis2=rlis
lis3=glis
for j in lis1:
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)

    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp

    lis2,lis3=lis3,lis2
    maxi = bisect.bisect_left(lis2,j)
    mink = bisect.bisect_right(lis3,j)
    tmp=maxi*(len(lis3)-mink)
    for i in lis2:
        if i>=j:
            break
        tj=j+1
        ti=i+1
        badk=tj+tj-ti
        badk-=1

        ind1=bisect.bisect_left(lis3,badk)
        ind2=bisect.bisect_right(lis3,badk)
        if ind1!=ind2:
            tmp-=1
    ans+=tmp

print(ans)

Solution (Python):
N = int(input())
K = str(input())

r = K.count("R")
g = K.count("G")
b = K.count("B")
total = r*g*b
n_remove = 0
for space in range(0, N//2 +1):
    for i in range(N- 2*(space+1)):
        try:
            pattern_remove = [K[i], K[i+space+1], K[i+(space*2)+ 2]]
            if set(pattern_remove) == set(["R", "B", "G"]):
                
                n_remove += 1
        except:
            continue
                
print(total - n_remove)

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

import collections
C = collections.Counter(S)

if len(C) < 3:
    print(0)
    exit()

count = 0

for i in range(N):
    j = 1
    while i - j >= 0 and i + j < N:
        if S[i-j] != S[i] and S[i+j] != S[i] and S[i-j] != S[i+j]:
            count += 1
        j += 1

ans = 1

for k, v in C.items():
    ans *= v

print(ans - count)

Solution (Python):





def solv(S):
    m = [[], [], []]
    for i, s in enumerate(S):
        if s == "R":
            m[0].append(i)
        elif s == "G":
            m[1].append(i)
        else:
            m[2].append(i)

    if len(m[0]) > len(m[1]):
        m[0], m[1] = m[1], m[0]
    if len(m[0]) > len(m[2]):
        m[0], m[2] = m[2], m[0]
    if len(m[1]) > len(m[2]):
        m[1], m[2] = m[2], m[1]

    C = set(m[2])
    ans = len(m[0]) * len(m[1]) * len(m[2])

    for a0 in m[1]:
        for b0 in m[0]:
            if a0 < b0:
                a, b = a0, b0
            else:
                a, b = b0, a0

            c1 = a - (b-a)
            c2 = b + (b-a)

            if c1 in C:
                ans -= 1
            if c2 in C:
                ans -= 1

            if (a+b) % 2 == 0:
                c3 = (a+b) // 2
                if c3 in C:
                    ans -= 1
    return ans


if __name__ == "__main__":

    _ = input()
    S = input()
    ans = solv(S)

    print(ans)

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

Solution (Python):
import collections

n = int(input())
s = str(input())
ans = 1

s_l = [s.count('R'), s.count('G'), s.count('B')]
for k in s_l:
    ans *= k


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

print(ans - cnt)

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)
lr, lg, lb = len(r), len(g), len(b)
ans = lr * lg * lb

for i in r:
    for j in g:
        a = 2 * j - i
        c = 2 * i - j
        if 0 <= a and a <= n-1 and s[a] == 'B':
            ans -= 1
        if 0 <= c and c <= n-1 and s[c] == 'B':
            ans -= 1
        if (i + j) % 2 == 0 and s[int((i + j) / 2)] == 'B':
            ans -= 1
print(ans)

Solution (Python):


MOD = 10**9
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

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


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

SN = []

for s in S:
    if s == 'R': SN.append(-1)
    elif s == 'G': SN.append(0)
    else: SN.append(1)

rem2 = ['RRR', 'GGG', 'BBB']

count = 0
for i in range(1, N-1):
    index = i
    left = i - 1
    right = i + 1
    f = True
    while f:
        if left == -1 or right == N:
            break
        if SN[left] == SN[index] and SN[index] == SN[right] == 0 : pass
        elif SN[left] + SN[index] + SN[right] == 0 :
            count+= 1
        
        left -= 1
        right += 1

print(ans-count)

Solution (Python):
n = int(input())
seq = input().strip()
ans = 0
r = set()
g = set()
b = set()
for i in range(len(seq)):
    if seq[i] == 'R':
        r.add(i)
    elif seq[i] == 'G':
        g.add(i)
    else:
        b.add(i)

ans = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        if (i+j) % 2 == 0 and ((i + j) //2) in b:
            ans -= 1
        if max(i,j) + (max(i,j) - min(i,j)) in b:
            ans -= 1
        if min(i,j) - (max(i,j) - min(i,j)) in b:
            ans -= 1
        
print(ans)

Solution (Python):
N=int(input())
S=input()
counter=[[0 for i in range(N)] for _ in range(3)]
if S[N-1]=="R":
  counter[0][N-1]=1
elif S[N-1]=="G":
  counter[1][N-1]=1
else:
  counter[2][N-1]=1
for i in range(1,N):
  if S[N-1-i]=="R":
    counter[0][N-1-i]=counter[0][N-i]+1
  elif S[N-1-i]=="G":
    counter[1][N-1-i]=counter[1][N-i]+1
  else:
    counter[2][N-1-i]=counter[2][N-i]+1
  counter[0][N-1-i]=max(counter[0][N-i],counter[0][N-1-i])
  counter[1][N-1-i]=max(counter[1][N-i],counter[1][N-1-i])
  counter[2][N-1-i]=max(counter[2][N-i],counter[2][N-1-i])
ans=0
for i in range(N-1):
  if S[i]=="R":
    for j in range(i+1,N):
      if S[j]=="G":
        ans+=counter[2][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="B":
            ans-=1
      elif S[j]=="B":
        ans+=counter[1][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="G":
            ans-=1
for i in range(N-1):
  if S[i]=="G":
    for j in range(i+1,N):
      if S[j]=="R":
        ans+=counter[2][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="B":
            ans-=1
      elif S[j]=="B":
        ans+=counter[0][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="R":
            ans-=1
for i in range(N-1):
  if S[i]=="B":
    for j in range(i+1,N):
      if S[j]=="R":
        ans+=counter[1][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="G":
            ans-=1
      elif S[j]=="G":
        ans+=counter[0][j]
        if 2*j-i<=N-1:
          if S[2*j-i]=="R":
            ans-=1
print(ans)

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

n_total = {'R':0,'G':0,'B':0}
n_0 = {'R':0,'G':0,'B':0}

for i,mid in enumerate(s):
    n_total[mid] += 1
n_1 = n_total.copy()

cnt = 0
for i,mid in enumerate(s):
    n_0[mid] += 1
    n_1[mid] -= 1
    if mid == 'R':
        cnt += n_0['G'] * n_1['B'] + n_0['B'] * n_1['G']
    if mid == 'G':
        cnt += n_0['R'] * n_1['B'] + n_0['B'] * n_1['R'] 
    if mid == 'B':
        cnt += n_0['G'] * n_1['R'] + n_0['R'] * n_1['G'] 
    for j in range(1,min(i, len(s) - 1 - i)+1):
        if (mid != s[i-j]) & (mid != s[i+j]) & (s[i+j] != s[i-j]):
            cnt -=1
print(cnt)

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

for i in range(n - 2):
  j = i + 1
  while((i + 1) + (j - i) * 2 <= n):
    if s[i] != s[j] and s[j] != s[i + (j - i) * 2] and s[i] != s[i + (j - i) * 2]:
      answer -= 1
    j += 1
 
print(answer)

Solution (Python):
N = int(input())
S = input()
memo = {key:[] for key in ("R","G","B")}
ans = 0
for i in range(N):
    memo[S[i]].append(i)
    for space in range(1,N+1):
        j = space + i
        k = space + j
        if(k<N):
            if(S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]):
                ans -= 1

ans += len(memo["R"])*len(memo["G"])*len(memo["B"])
print(ans)

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

R.sort()
G.sort()
B.sort()

from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
 
ans = len(R)*len(G)*len(B)
if ans > 0:
    for r in R:
        for g in G:
            for p in [r*2-g,g*2-r,(r+g)/2]:
                if p<=B[-1] and p>=B[0]:
                    ind = bisect_left(B,p)
                    if p == B[ind]:
                        ans -= 1

print(ans)

Solution (Python):
from bisect import bisect_right

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

d = [[0] * N for _ in range(3)]
for i in range(3):
    for j in range(N):
        d[i][j] = int(S[j] == "RGB"[i])
        
cumsum = [[0] * (N + 1) for _ in range(3)]
for i in range(3):
    for j in range(N):
        cumsum[i][j + 1] = cumsum[i][j] + d[i][j]

cnt = [len(v) for v in d]
ans = 0
for i in range(1, N - 1):
    for j in range(i + 1, N):
        
        if S[i - 1] == S[j - 1]:
            continue

        
        s = (set("RGB") - set([S[i - 1], S[j - 1]])).pop()
        idx = "RGB".find(s)
        ans += cumsum[idx][-1] - cumsum[idx][j]
        
        k = 2 * j - i
        if k <= N and d[idx][k - 1]:
            ans -= 1
        
print(ans)

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

r = len([i for i in s if i == "R"])
g = len([i for i in s if i == "G"])
b = len(s)- r-g

res = r *g * b
count = 0
between = 1
isLoop = True
while isLoop:
    for start in range(between):
        tg = s[start::between]
        if start == 0 and len(tg) < 3:
            isLoop = False
            break
        res -= tg.count("RGB") \
        + tg.count("RBG") \
        + tg.count("GRB") \
        + tg.count("GBR") \
        + tg.count("BRG") \
        + tg.count("BGR") 
    between += 1

print(res)