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

Solution (Python):
import sys
input = sys.stdin.readline
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 j in range(i+1,N):
        k=j+j-i
        if k>=N:
            continue
        if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
            ans-=1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
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 j in range(i+1,N):
        k=j+j-i
        if k>=N:
            continue
        if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
            ans-=1
print(ans)

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

R = 0
G = 0
B = 0

for i in range(n):
    if s[i] == "R":
        R += 1
    elif s[i] == "G":
        G += 1
    elif s[i] == "B":
        B += 1

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

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

Solution (Python):
n= int(input())
s_list= list(input())
count=0
rr,bb,gg=0,0,0
for i in range(0,n):
    if s_list[i]=="R":
        rr=rr+1
    elif s_list[i]=="G":
        gg=gg+1
    else:
        bb=bb+1
sum=rr*gg*bb

for p in range(0,n-2):
    for q in range(p+1,n-1):
        if s_list[p] == s_list[q]:
            continue
        elif 2*q-p<n and s_list[2*q-p]!=s_list[p] and s_list[2*q-p]!=s_list[q]:
            count= count+1
print(sum-count)

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

Solution (Python):
number = int(input())
character = input()
rnumber = character.count('R')
gnumber = character.count('G')
bnumber = character.count('B')
count = rnumber*gnumber*bnumber

for j in range(1, number):
    for i in range(j):
        k = j + (j - i)
        if k >= number:
            continue    
        if character[i] ==character[j]:
            continue
        if character[i] == character[k]:
            continue
        if character[j] == character[k]:
            continue
        count -= 1


print(count)

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

def main():
    N = int(input())
    S = input()
    CNTR = 0
    CNTG = 0
    CNTB = 0
    for i in range(N):
        if S[i] == 'R':
            CNTR += 1
        if S[i] == 'G':
            CNTG += 1
        if S[i] == 'B':
            CNTB += 1
    CONB = CNTR * CNTG * CNTB
    CNT = 0
    for i in range(N):
        for j in range(i,N):
            if S[j] != S[i]:
                k = j + (j-i)
                if k > N-1:
                    continue
                if S[k] != S[i] and S[k] != S[j]:
                    CNT += 1
    print(CONB - CNT)


if __name__ == '__main__':
    main()

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

def main():
    N = int(input())
    S = input()
    CNTR = 0
    CNTG = 0
    CNTB = 0
    for i in range(N):
        if S[i] == 'R':
            CNTR += 1
        if S[i] == 'G':
            CNTG += 1
        if S[i] == 'B':
            CNTB += 1
    CONB = CNTR * CNTG * CNTB
    CNT = 0
    for i in range(N):
        for j in range(i,N):
            if S[j] != S[i]:
                k = j + (j-i)
                if k > N-1:
                    continue
                if S[k] != S[i] and S[k] != S[j]:
                    CNT += 1
    print(CONB - CNT)


if __name__ == '__main__':
    main()

Solution (Python):


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

count = 0
r, g, b = 0, 0, 0

for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    elif i == 'B':
        b += 1

count = r * g * b

for i in range(n-2):
    si = s[i]
    for j in range(i+1,n-1):
        sj = s[j]
        nk = 2 * j - i
        if not nk < n:
            break
        snk = s[nk]
        if {si, sj, snk} == {'R','G','B'}:
            count -= 1

print("{}".format(count))

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

N = int(input())
S = input()
CNTR = 0
CNTG = 0
CNTB = 0
for i in range(N):
    if S[i] == 'R':
        CNTR += 1
    if S[i] == 'G':
        CNTG += 1
    if S[i] == 'B':
        CNTB += 1
CONB = CNTR * CNTG * CNTB
CNT = 0
for i in range(N):
    for j in range(i,N):
        if S[j] != S[i]:
            k = j + (j-i)
            if k > N-1:
                continue
            if S[k] != S[i] and S[k] != S[j]:
                CNT += 1
print(CONB - CNT)

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

dic = {k: 0 for k in "RGB"}

for i in range(n):
  dic[s[i]] += 1
  
res = dic['R'] * dic['G'] * dic['B']

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

print(res)

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

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

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

print(ans-tmp)

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

def main():
    n = int(input())
    s_li = list(input().rstrip())

    r_li = []
    g_li = []
    b_li = []

    for i in range(n):
        if s_li[i] == 'R':
            r_li.append(i)
        elif s_li[i] == 'G':
            g_li.append(i)
        elif s_li[i] == 'B':
            b_li.append(i)

    if (len(r_li) < 1 or len(g_li) < 1) or len(b_li) < 1:
        print(0)
    else:
        all_c = len(r_li) * len(g_li) * len(b_li)

        for i in range(n):
            for j in range(i+1,n):
                k = 2*j-i
                if k >= n:
                    continue

                if (s_li[i] != s_li[j] and s_li[j] != s_li[k]) and s_li[i] != s_li[k]:
                    all_c -= 1

        print(all_c)

if __name__ == '__main__':
    main()

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

arr = []
R = len(list(filter(lambda x:x == 'R', s)))
G = len(list(filter(lambda x:x == 'G', s)))
B = len(list(filter(lambda x:x == 'B', s)))
tot = R*G*B

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

print(tot)

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

Solution (Python):
N=int(input())
S=input()
R=S.count("R")
G=S.count("G")
B=S.count("B")
ans=R*G*B
for i in range(N):
    for j in range(i+1,N):
        k=j+j-i
        if k>=N:
            continue
        if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
            ans-=1
print(ans)

Solution (Python):
def main():
    n = int(input())
    s = input()
    
    rs = s.count('R')
    gs = s.count('G')
    bs = s.count('B')
    ans = rs * gs * bs

    
    for j in range(n):
        for i in range(j):  
            k = j + (j - i)
            if k >= n:
                continue
            if s[i] == s[j]:
                continue
            if s[i] == s[k]:
                continue
            if s[j] == s[k]:
                continue

            
            ans -= 1

    return ans


if __name__ == '__main__':
    print(main())

Solution (Python):
from collections import defaultdict
counts = defaultdict(lambda: 0)

n = int(input())
s = input()
ans = 0

for i, c in enumerate(s):
    counts[c] += 1
    j = 1
    while i - j >= 0 and i + j <= n - 1:
        if s[i - j] != s[i] and s[i] != s[i + j] and s[i - j] != s[i + j]:
            ans -= 1
        j += 1
print(ans + counts['R'] * counts['G'] * counts['B'])

Solution (Python):
def main():
  N = int(input())
  S = input()
  R = 0
  G = 0
  B = 0
  nsum = 0
  for s in S:
      if s == "R":
          R += 1
      elif s == "G":
          G += 1
      else:
          B += 1
  count = 0
  for i in range(N):
      for k in range(i+1,N):
          if not S[i] == S[k]:
              if 2*k - i <= N - 1:
                  if S[i] != S[2*k - i] and S[k] != S[2*k - i]:
                      count += 1
  print(R*G*B - count)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = input()
R = 0
G = 0
B = 0
nsum = 0
for s in S:
    if s == "R":
        R += 1
    elif s == "G":
        G += 1
    else:
        B += 1
count = 0
for i in range(N):
    for k in range(i+1,N):
        if not S[i] == S[k]:
            if 2*k - i <= N - 1:
                if S[i] != S[2*k - i] and S[k] != S[2*k - i]:
                    count += 1
print(R*G*B - count)

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

all = S.count("R") * S.count("G") * S.count("B")
count = 0
for i in range(N):
    for j in range(1,N):
        k = 2*j - i
        if k < N and i < j:
            if (S[i] != S[j]) and (S[j] != S[k]) and (S[k] != S[i]):
                count += 1
print(all-count)

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

Solution (Python):
def main():
    n = int(input())
    s = input()
    
    rs = s.count('R')
    gs = s.count('G')
    bs = s.count('B')
    ans = rs * gs * bs

    
    for j in range(n):
        for i in range(j):  
            k = j + (j - i)
            if k >= n:
                continue
            if s[i] == s[j]:
                continue
            if s[i] == s[k]:
                continue
            if s[j] == s[k]:
                continue

            
            ans -= 1

    return ans


if __name__ == '__main__':
    print(main())

Solution (Python):
n = int(input())
s = input()
rl = s.count('R')
gl = s.count('G')
bl = s.count('B')
rr = 0
gr = 0
br = 0
if s[0] == 'R':
    rl -= 1
    rr += 1
elif s[0] == 'G':
    gl -= 1
    gr += 1
else:
    bl -= 1
    br += 1

ans = 0
for i in range(1, n-1):
    for j in range(1, n//2+1):
        if i-j < 0 or i+j > n-1:
            break
        if s[i+j] != s[i-j] and s[i+j] != s[i] and s[i-j] != s[i]:
            ans -= 1
    if s[i] == 'R':
        ans += bl*gr + br*gl
        rl -= 1
        rr += 1
    elif s[i] == 'G':
        ans += rr*bl + rl*br
        gl -= 1
        gr += 1
    else:
        ans += gr*rl + gl*rr
        bl -= 1
        br += 1
print(ans)

Solution (Python):
def main():
    n = int(input())
    l = list(input())
    ans = l.count('R') * l.count('G') * l.count('B')
    for c in range(n):
        center = l[c]
        for dif in range(n):
            if dif + c > n - 1 or c - dif < 0:
                continue
            left = l[c - dif]
            right = l[dif + c]
            if left != center and center != right and right != left:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

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

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

print(ans)

Solution (Python):
n = int(input())
s = input()
rl = s.count('R')
gl = s.count('G')
bl = s.count('B')
rr = 0
gr = 0
br = 0
if s[0] == 'R':
    rl -= 1
    rr += 1
elif s[0] == 'G':
    gl -= 1
    gr += 1
else:
    bl -= 1
    br += 1

ans = 0
for i in range(1, n-1):
    for j in range(1, n//2+1):
        if i-j < 0 or i+j > n-1:
            break
        if s[i+j] != s[i-j] and s[i+j] != s[i] and s[i-j] != s[i]:
            ans -= 1
    if s[i] == 'R':
        ans += bl*gr + br*gl
        rl -= 1
        rr += 1
    elif s[i] == 'G':
        ans += rr*bl + rl*br
        gl -= 1
        gr += 1
    else:
        ans += gr*rl + gl*rr
        bl -= 1
        br += 1
print(ans)

Solution (Python):
n = int(input())
s = input()
rl = s.count('R')
gl = s.count('G')
bl = s.count('B')
rr = 0
gr = 0
br = 0
if s[0] == 'R':
    rl -= 1
    rr += 1
elif s[0] == 'G':
    gl -= 1
    gr += 1
else:
    bl -= 1
    br += 1

ans = 0
for i in range(1, n-1):
    for j in range(1, n//2+1):
        if i-j < 0 or i+j > n-1:
            break
        if s[i+j] != s[i-j] and s[i+j] != s[i] and s[i-j] != s[i]:
            ans -= 1
    if s[i] == 'R':
        ans += bl*gr + br*gl
        rl -= 1
        rr += 1
    elif s[i] == 'G':
        ans += rr*bl + rl*br
        gl -= 1
        gr += 1
    else:
        ans += gr*rl + gl*rr
        bl -= 1
        br += 1
print(ans)

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

Solution (Python):
n = int(input())
s = input()
rl = s.count('R')
gl = s.count('G')
bl = s.count('B')
rr = 0
gr = 0
br = 0
if s[0] == 'R':
    rl -= 1
    rr += 1
elif s[0] == 'G':
    gl -= 1
    gr += 1
else:
    bl -= 1
    br += 1

ans = 0
for i in range(1, n-1):
    for j in range(1, n//2+1):
        if i-j < 0 or i+j > n-1:
            break
        if s[i+j] != s[i-j] and s[i+j] != s[i] and s[i-j] != s[i]:
            ans -= 1
    if s[i] == 'R':
        ans += bl*gr + br*gl
        rl -= 1
        rr += 1
    elif s[i] == 'G':
        ans += rr*bl + rl*br
        gl -= 1
        gr += 1
    else:
        ans += gr*rl + gl*rr
        bl -= 1
        br += 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
dib=0
m=0

for i in range(N-2):
    m=(N+i)//2+1
    for j in range(i+1,m):
            dib=j-i
            if j+dib<=N-1:
                if S[i]!=S[j] and S[i]!=S[j+dib] and S[j]!=S[j+dib]:
                    ans=ans-1
            
print(ans)

Solution (Python):
N = int(input())
S = input()
CNTR = 0
CNTG = 0
CNTB = 0
for i in range(N):
    if S[i] == 'R':
        CNTR += 1
    if S[i] == 'G':
        CNTG += 1
    if S[i] == 'B':
        CNTB += 1
CONB = CNTR * CNTG * CNTB
CNT = 0
for i in range(N):
    for j in range(i,N):
        if S[j] != S[i]:
            k = j + (j-i)
            if k > N-1:
                continue
            if S[k] != S[i] and S[k] != S[j]:
                CNT += 1
print(CONB - CNT)

Solution (Python):
N = int(input())
S = input()
CNTR = 0
CNTG = 0
CNTB = 0
for i in range(N):
    if S[i] == 'R':
        CNTR += 1
    if S[i] == 'G':
        CNTG += 1
    if S[i] == 'B':
        CNTB += 1
CONB = CNTR * CNTG * CNTB
CNT = 0
for i in range(N):
    for j in range(i,N):
        if S[j] != S[i]:
            k = j + (j-i)
            if k > N-1:
                continue
            if S[k] != S[i] and S[k] != S[j]:
                CNT += 1
print(CONB - CNT)

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

num_r = S.count('R')
num_g = S.count('G')
num_b = S.count('B')

ans = num_r * num_g * num_b

for i in range(0, N) :
  for temp in range(1, int((N-i+1)/2)) :
    j = i + temp
    k = i + 2 * temp
    c1 = S[i]
    c2 = S[j]
    c3 = S[k]
    if((c1 != c2) and (c2 != c3) and (c3 != c1)) :
      ans -= 1
        
        
print(ans)

Solution (Python):
N = int(input())  
S = input()  
L = {
    'R': tuple(i for i, v in enumerate(S) if v == 'R'),
    'G': tuple(i for i, v in enumerate(S) if v == 'G'),
    'B': tuple(i for i, v in enumerate(S) if v == 'B')
}
R_length = len(L['R'])
G_length = len(L['G'])
B_length = len(L['B'])

ans = R_length * G_length * B_length
for i in range(N):
    for j in range(i+1, N):
        k = j + j - i
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

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

r_cnt=s.count("R")
g_cnt=s.count("G")
b_cnt=s.count("B")

ans=r_cnt*g_cnt*b_cnt

for i in range(n):
    for d in range(n):
        j=i+d
        k=j+d
        if k>=n:
            break
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            ans-=1

print(ans)

Solution (Python):
from collections import Counter

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

c = Counter(S)

ans = c['R'] * c['G'] * c['B']

for d in range(1, 4000):
    for i in range(N):
        j = i + d
        k = j + d
        if k >= N:
            continue

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

print(ans)

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

r_cnt=s.count("R")
g_cnt=s.count("G")
b_cnt=s.count("B")

ans=r_cnt*g_cnt*b_cnt

for i in range(n):
    for d in range(n):
        j=i+d
        k=j+d
        if k>=n:
            break
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            ans-=1

print(ans)

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

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

Solution (Python):
def solve(n, s):
    D = {"R": 0, "G": 0, "B": 0}
    for c in s:
        D[c] += 1
    ans = D["R"] * D["G"] * D["B"]
    for i in range(n):
        for j in range(i+1,(i+n+1)//2):
            k = 2*j - i
            if (s[i] != s[j]) and (s[j] != s[k]) and (s[k] != s[i]):
                ans -= 1
    return ans

n = int(input())
s = input()
print(solve(n, s))

Solution (Python):
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
S=input()
rt,gt,bt=S.count("R"),S.count("G"),S.count("B")
R,G,B=[],[],[]
rc,gc,bc=0,0,0
ans=0
for i in range(n):
  if S[i]=="R":
    ans+=gc*(bt-bc)+bc*(gt-gc)
    for b in B:
      if 2*i-b<n and S[2*i-b]=="G":
        ans-=1
    for g in G:
      if 2*i-g<n and S[2*i-g]=="B":
        ans-=1
    R.append(i)
    rc+=1
  elif S[i]=="G":
    ans+=rc*(bt-bc)+bc*(rt-rc)
    for b in B:
      if 2*i-b<n and S[2*i-b]=="R":
        ans-=1
    for r in R:
      if 2*i-r<n and S[2*i-r]=="B":
        ans-=1
    G.append(i)
    gc+=1
  else:
    ans+=gc*(rt-rc)+rc*(gt-gc)
    for r in R:
      if 2*i-r<n and S[2*i-r]=="G":
        ans-=1
    for g in G:
      if 2*i-g<n and S[2*i-g]=="R":
        ans-=1
    B.append(i)
    bc+=1
print(ans)

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

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

for i in range(n):
    for d in range(n):
        if i+(2*d) >= n: break
        if s[i] != s[i+d] and s[i] != s[i+(2*d)] and s[i+d] != s[i+(2*d)]:
            ans -= 1
            
print(ans)

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

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

for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >= n: break
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ans -= 1

print(ans)

Solution (Python):
import time
start = time.time()
import sys


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













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


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

print(ans)

Solution (Python):
n = int(input())
s = input()
S = list(s)
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r * g * b
for k in range(n):
        for j in range(k):
            i = 2*j - k
            if i >= 0 and i < j and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
sum = 0
r = 0
g = 0
b = 0

for i in range(n):
    if s[i] == "R":
        r += 1
    elif s[i] == "G":
        g += 1
    else:
        b += 1
sum += r*g*b

for i in range(n):
    wi = (n-i+1)//2
    for w in range(1,wi):
        if s[i] == s[i+w] or s[i+w] == s[i+2*w] or s[i] == s[i+2*w]:
            continue
        else:
            sum -= 1

print(sum)

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

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

for i in range(N):
  for j in range(i+1, N):
    k = j + j - i
    if k < N :
      if (S[i] != S[j]) and (S[j] != S[k]) and (S[k] != S[i]) :
        ans -= 1

print(ans)

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

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

for i in range(N):
  for j in range(i+1, N):
    k = j + j - i
    if k < N :
      if (S[i] != S[j]) and (S[j] != S[k]) and (S[k] != S[i]) :
        ans -= 1

print(ans)

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

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

for i in range(N):
  for j in range(i+1, N):
    k = j + j - i
    if k < N :
      if (S[i] == S[j]) or (S[j] == S[k]) or (S[k] == S[i]) :
        continue
      ans -= 1

print(ans)

Solution (Python):
from collections import Counter

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

cnt = Counter(S)

ans = cnt['R'] * cnt['G'] * cnt['B']

for i in range(N):
  for j in range(i+1, N):
    k = j + j - i
    if k < N :
      if (S[i] == S[j]) or (S[j] == S[k]) or (S[k] == S[i]) :
        continue
      ans -= 1

print(ans)

Solution (Python):
N = int(input())
S = input()
if N>3 :
  total = S.count("R")*S.count("G")*S.count("B")
  for i in range(0,N-2):
    for j in range(i+1,N-1):
      if 2*j-i<N:
        if S[i]!=S[j] and S[j]!=S[2*j-i] and S[2*j-i]!=S[i]:
          total-=1
  print(total)
else:
  print(0)

Solution (Python):


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

rnum = s.count('R')
gnum = s.count('G')
bnum = s.count('B')
ans = rnum * gnum * bnum
for i in range(n):
    for j in range(n):
        d = i+j 
        f = d+j 
        if f>=n:
            break
        if s[i]!=s[d] and s[d]!=s[f] and s[f]!=s[i]:
            ans -= 1
print(ans)

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

Solution (Python):
import collections

n = int(input())
s = input()
res = 0








num = collections.Counter(s)

num_r = num["R"]
num_g = num["G"]
num_b = n - num_r - num_g
count_2 = 0
for x in range(1,int(n/2)+1):
    for i in range(n-2*x):
        
        
        
        if (i + 2 * x < n and s[i] != s[i + x] and s[i + x] != s[i + 2 * x] and s[i] != s[i + 2 * x] ):
            count_2 += 1
            
            
            

res = num_r * num_g * num_b - count_2



print(res)

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













r = 0
g = 0
b = 0

for t in s:
    if t == "R":
        r += 1
    elif t == "G":
        g += 1

b = N - r - g
cnt = r * g * b

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

print(cnt)

Solution (Python):



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


def main():
    ans = S.count("R") * S.count("G") * S.count("B")
    
    lim = (N - 1) // 2 + 1
    for i in range(1, lim):
        lim2 = N - 2 * i
        for j in range(lim2):
            if S[j] != S[j+i] and S[j+i] != S[j+2*i] and S[j] != S[j+2*i]:
                ans -= 1
    
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
S = input()
count = S.count("R") * S.count("G") * S.count("B")


for j in range(N):
    for i in range(j):
        k = 2 * j - i
        if k >= N:
            continue
        if (S[i] != S[j]) and (S[i] != S[k]) and (S[j] != S[k]):
            count -= 1
print(count)

Solution (Python):
n = int(input())
s = input()
c = s.count('R') * s.count('G') * s.count('B')
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        k = j + (j - i)
        if k >= n:
            continue
        if s[i] == s[j] or s[j] == s[k] or s[k] == s[i]:
            continue
        c -= 1
print(c)

Solution (Python):
n=int(input())
ln=input()
r=set()
g=set()
b=set()
for i,alp in enumerate(ln):
    if alp=="R":
        r.add(i)
    elif alp=="G":
        g.add(i)
    elif alp=="B":
        b.add(i)
max=len(r)*len(g)*len(b)
cnt=0

for i in range(n):
    for j in range(i+1,n):
        k=j+j-i
        if k<n:
            if ln[i]==ln[j] or ln[j]==ln[k] or ln[i]==ln[k]:
                continue
            else:
                cnt+=1

max-=cnt
print(max)

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    N = Scanner.int()
    S = Scanner.string()
    ans = S.count('R') * S.count('G') * S.count('B')
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            k = 2 * j - i
            if k >= N:
                continue
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
    print(ans)


def main():
    
    solve()


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter


def solve():
    n = int(input())
    s = input()
    cnt = Counter(s)
    ans = cnt['R'] * cnt['G'] * cnt['B']
    for i in range(n):
        for j in range(i + 1, n):
            k = j + j - i
            if k < n:
                if s[i] == s[j] or s[i] == s[k] or s[j] == s[k]:
                    continue
                ans -= 1
    return ans


print(solve())

Solution (Python):
N = int(input())
S = list(input())
arr_R = []
arr_G = []
arr_B = []

for idx,s in enumerate(S):
  if s=="R":
    arr_R.append(idx+1)
  elif s=="G":
    arr_G.append(idx+1)
  else:
    arr_B.append(idx+1)

ans = len(arr_R)*len(arr_G)*len(arr_B)
omit = 0

for i in range(N):
  for j in range(i+1,N):
    k = 2*j-i
    if k<N:
      if not (S[i]==S[j] or S[j]==S[k] or S[k]==S[i]):
        omit += 1
print(ans-omit)

Solution (Python):
def solve():
    n = int(input())
    s = input()
    cnt = [0] * 3
    for i in range(n):
        cnt[ord(s[i]) % 3] += 1
    ans = 1
    for i in range(3):
        ans *= cnt[i]
    for i in range(n):
        for j in range(i + 1, n):
            k = j + j - i
            if k < n:
                if s[i] == s[j] or s[i] == s[k] or s[j] == s[k]:
                    continue
                ans -= 1
    return ans


print(solve())

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

    
    
    
    s = [ord(c) % 3 for c in input()]
    ans = 1
    ans = s.count(0) * s.count(1) * s.count(2)
    for i in range(n):
        for j in range(i + 1, n):
            k = j + j - i
            if k < n:
                if s[i] == s[j] or s[i] == s[k] or s[j] == s[k]:
                    continue
                ans -= 1
    return ans


print(solve())

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

    
    
    
    s = [ord(c) % 3 for c in input()]

    accs = [[0] * (n + 1) for _ in range(3)]
    for i in range(3):
        for j in range(n):
            if i != s[j]:
                accs[i][j + 1] = accs[i][j]
            else:
                accs[i][j + 1] = accs[i][j] + 1

    ans = 0
    for i in range(n):
        for j in range(i + 1, n):
            if s[i] == s[j]:
                continue
            residual = ({0, 1, 2} - {s[i], s[j]}).pop()
            ans += accs[residual][n] - accs[residual][j]
            if j + j - i + 1 <= n:
                ans -= accs[residual][j + j - i + 1] - accs[residual][j + j - i]
    return ans


print(solve())

Solution (Python):













mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    

    n = int(input())
    S = input()
    cnt = S.count('R')*S.count('G')*S.count('B') 
    
    
    for i in range(n):
        for j in range(i+1,n):
            if S[i] == S[j]:
                continue
            k = 2*j - i 
            if k >= n or S[k] == S[i] or S[k] == S[j]: 
            
            
                continue
            cnt -= 1
    print(cnt)


if __name__ == '__main__':
  main()

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

r = g = b = 0
for i in s:
    if i == "R":
        r += 1
    elif i == "G":
        g += 1
    elif i == "B":
        b += 1
ans = r * g * b

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

print(ans)

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

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

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

print(ans)