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()
r = S.count('R')
g = S.count('G')
b = S.count('B')
cnt = 0

for i in range(n):
  for j in range(i+1,n):
	
    k = 2*j-i
    if k >= n or k <= j:
      break
    if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
      cnt += 1
print(r*g*b - cnt)

Solution (Python):
n = int(input())
s = list(input())
a_li = []
b_li = []
c_li = []
for i,ss in enumerate(s):
    if ss == 'R':
        a_li.append(i+1)
    elif ss == 'G':
        b_li.append(i+1)
    else:
        c_li.append(i+1)
count = 0
for i in range(1,n+1):
    for d in range(1,n):
        if i + 2*d > n:
            break
        if s[i-1] != s[i+d-1] and s[i+d-1] != s[i+2*d-1] and s[i-1] != s[i+2*d-1]:
            count += 1


print(len(a_li)*len(b_li)*len(c_li)-count)

Solution (Python):
n = int(input())
s = list(input())
a_li = []
b_li = []
c_li = []
for i,ss in enumerate(s):
    if ss == 'R':
        a_li.append(i+1)
    elif ss == 'G':
        b_li.append(i+1)
    else:
        c_li.append(i+1)
count = 0
for i in range(1,n+1):
    for d in range(1,n):
        if i + 2*d > n:
            break
        if s[i-1] != s[i+d-1] and s[i+d-1] != s[i+2*d-1] and s[i-1] != s[i+2*d-1]:
            count += 1


print(len(a_li)*len(b_li)*len(c_li)-count)

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

R = s.count('R')
G = s.count('G')
B = n - R - G

err = 0
for i in range(n - 2):
    span = 1
    while i + 2 * span < n:
        if s[i] != s[i + span] and s[i + span] != s[i + 2 * span] and s[i + 2 * span] != s[i]:
            err += 1
        span += 1

print(R * G * B - err)

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

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

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

print(ans)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N = i_i()
S = i_s()

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

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

print((r * g * b) - cnt)

Solution (Python):
def binary_search(list,target):

	result = -1
	left = 0
	right = len(list) - 1

	while left <= right:
		center = int((left + right)/2)
		if list[center] == target:
			result = center
			break
		elif list[center] < target:
			left = center + 1
		elif list[center] > target:
			right = center - 1

	if result == -1:
		return result
	else:
		return result


if __name__=="__main__":
    n=int(input())
    s=list(input())
    rlist=[]
    glist=[]
    blist=[]
    ans=0
    if len(s)<=3:
        print(0)
        exit()
    for i in range(n):
        if s[i]=="R":
            rlist.append(i)
        elif s[i]=="G":
            glist.append(i)
        else:
            blist.append(i)
    if len(rlist)==0 or len(glist)==0 or len(blist)==0:
        print(0)
        exit()
    ans+=len(rlist)*len(glist)*len(blist)
    for i in range(n-2):
        for j in range(i+1,n-1):
            k=2*j-i
            if s[i]==s[j]:
                continue
            elif k>len(s)-1:
                continue
            else:
                if s[k]!=s[i] and s[k]!=s[j]:
                    ans-=1

    print(ans)

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

Solution (Python):
n = int(input())
s = input()
cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        k = 2*j - i
        if j < k < n and len({s[i], s[j], s[k]}) == 3:
            cnt += 1
print(s.count('R') * s.count('G') * s.count('B') - cnt)

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

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

r, g, b = map(s.count, 'RGB')

print(r * g * b - sum(1 for i, c1 in enumerate(s) for j, c2 in enumerate(s[i::2]) if c1 != c2 != s[i + j] != c1))

Solution (Python):
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor



dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]


N = int(input())
S = input()
L = [None]*3
L[0] = []
L[1] = []
L[2] = []
for i in range(N):
    if S[i] == 'R':
        L[0].append(i)
    if S[i] == 'G':
        L[1].append(i)
    if S[i] == 'B':
        L[2].append(i)
cnt = 0
wa = [None]*3
for i in range(3):
    wa[i] = [0]*(N+1)
for i in range(3):
    for j in range(N):
        if S[j] == "RGB"[i]:
            wa[i][j+1] = 1
for i in range(3):
    for j in range(N):
        wa[i][j+1] = wa[i][j]+wa[i][j+1]
for x in itertools.permutations([0, 1, 2]):
    r = L[x[0]]
    g = L[x[1]]
    b = L[x[2]]
    for i in range(len(r)):
        for j in range(len(g)):
            if r[i] < g[j]:
                cnt += wa[x[2]][N]-wa[x[2]][g[j]+1]
                if len(S) > (g[j]-r[i])+g[j] and S[(g[j]-r[i])+g[j]] == "RGB"[x[2]]:
                    cnt -= 1
print(cnt)

Solution (Python):
N =int(input())
S = str(input())
A = 0
R = []
G = []
B = []
for i in range(len(S)):
  if S[i] == "R":
    R.append(i+1)
  elif S[i] == "G":
    G.append(i+1)
  else:
    B.append(i+1)

A = len(R)*len(G)*len(B)
for i in range(N):
  for j in range(i+1,N):
    k = j + (j-i)
    if k >= N:
      break
    if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
      A -= 1
print(A)

Solution (Python):
n = int(input())
s = input()
r = 0
if n > 2:
    r = s.count('R') * s.count('G') * s.count('B')
    for i in range(n - 2):
        k = i + 2
        for j in range(i + 1, n - 1):
            k += 2 if j > i + 1 else 0
            if k >= n:
                break
            r -= 1 if s[i] != s[j] and s[i] != s[k] and s[j] != s[k] else 0
print(r)

Solution (Python):




from collections import deque
N=int(input())
S=input()
R=deque()
G=deque()
B=deque()
for i,s in enumerate(S):
    if s=='R':
        R.append(i)
    if s == 'G':
        G.append(i)
    if s == 'B':
        B.append(i)

rgb = len(R)*len(G)*len(B)
import bisect

R_one = [0 for _ in range(N+3)]
G_one = [0 for _ in range(N+3)]
B_one = [0 for _ in range(N+3)]

for r in R:
    R_one[r] = 1
for g in G:
    G_one[g] = 1
for b in B:
    B_one[b] = 1

for i in range(0,N):
    if i in R:
        lest1 = G_one
        lest2 = B_one
    elif i in G:
        lest1 = B_one
        lest2 = R_one
    else:
        lest1 = R_one
        lest2 = G_one
    max_j_width = int((N-1-i)/2)+1
    for j in range(1,max_j_width+1):
        if lest1[i+j]== lest2[i+2*j] == 1:
            rgb-=1
        elif lest2[i + j] == lest1[i + 2 * j] == 1:
            rgb-=1
print(rgb)

Solution (Python):
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):
        k = 2*j - i
        if j < k < n and len({s[i], s[j], s[k]}) == 3:
            cnt -= 1
print(cnt)

Solution (Python):
N=int(input())
S=input()
r=g=b=0

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

ans = r*g*b

for i in range(N-2):
    for j in range(i+1,N-1):
        if 2*j-i>N-1:
            continue
        else:
            first=S[i]
            second=S[j]
            third=S[2*j-i]
            if first!=second and second!=third and first!=third:
                ans-=1
print(ans)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]


def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

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

ruisekiR = [0] * (n+1)
ruisekiG = [0] * (n+1)
ruisekiB = [0] * (n+1)

for i,c in enumerate(s):
    ruisekiR[i+1] = ruisekiR[i] + ( 1 if c == "R" else 0)
    ruisekiG[i+1] = ruisekiG[i] + ( 1 if c == "G" else 0)
    ruisekiB[i+1] = ruisekiB[i] + ( 1 if c == "B" else 0)





ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        c = "R"
        if "R" in (s[i], s[j]):
            c = "G"
            if "G" in (s[i], s[j]):
                c = "B"

        

        if c == "R":
            ans += (ruisekiR[n] - ruisekiR[j])
        if c == "G":
            ans += (ruisekiG[n] - ruisekiG[j])
        if c == "B":
            ans += (ruisekiB[n] - ruisekiB[j])

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

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):
        if (j + j - i < N) and ({S[i], S[j], S[j + j - i]} == {'R', 'G', 'B'}):
            ans -= 1

print(ans)

Solution (Python):
N=int(input())
S=input().rstrip()
t=S.count('R')*S.count('G')*S.count('B')
d=0
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[i]==S[k] or S[j]==S[k]: continue
    d+=1
print(t-d)

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

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

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

print((r * g * b) - count)

Solution (Python):
N = int(input())
arr = list(input())
a = arr.count("R")
b = arr.count("G")
c = arr.count("B")
count = a * b * c

for i in range(N - 2):
  for j in range(i + 1, N - 1):
    k = i + (j - i) * 2
    if k < N and arr[i] != arr[j] and arr[j] != arr[k] and arr[k] != arr[i]:
      count -= 1
print(count)

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


r, g, b = 0, 0, 0
for i in range(N):
    if S[i] == 'R':
        r += 1
    elif S[i] == 'G':
        g += 1
    else:
        b += 1

ans = r*g*b 

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

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

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

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

print((r*g*b)-cnt)

Solution (Python):



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


r_count = 0
g_count = 0
b_count = 0
exception_count = 0


for i in range(N):
    if S[i]=='R':
        r_count += 1
    elif S[i]=='G':
        g_count += 1
    elif S[i]=='B':
        b_count += 1
ans_1 = r_count * g_count * b_count


for i in range(N-2):
    for j in range(i+1,N-1):
        k = 2 * j - i
        if k>=N:
            continue
        if not S[i]==S[j] and not S[j]==S[k] and not S[i]==S[k]:
            exception_count += 1


ans = ans_1 - exception_count
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()
r = S.count('R')
b = S.count('B')
g = S.count('G')
x = r*b*g

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

print(x-count)

Solution (Python):
n = int(input())
s = input()
ans = s.count('R') * s.count('G') * s.count('B')
for i in range(n-2):
    for j in range(i+1, n-1):
        if 2*j - i >= n:
            break
        elif s[i] != s[j] and s[j] != s[(2*j-i)] and s[(2*j-i)] != s[i]:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = str(input())
r, g, b = map(s.count, "RGB")
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        k = 2*j - i
        if k<n and len(set([s[i], s[j], s[k]])) == 3:
            ans -= 1
print(ans)

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

R_count = S.count('R')
G_count = S.count('G')
B_count = S.count('B')

ans = R_count * G_count * B_count

for i in range(N-2):
    for d in range(1,N):
        if i+2*d >= N:
            break
        else:
            j = i+d
            k = j+d
            
            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_count = S.count('R')
G_count = S.count('G')
B_count = S.count('B')

ans = R_count * G_count * B_count

for i in range(N-2):
    for d in range(1,N):
        if i+2*d >= N:
            break
        else:
            j = i+d
            k = j+d
            
            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 = 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
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        cur = j-i
        if j+cur <= n-1:
            if S[i] != S[j] and S[j] != S[j+cur] and S[j+cur] != 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):
    for j in range(N):
        if i-j<0 or i+j >= N:
            break
        x = S[i-j]
        y = S[i]
        z = S[i+j]

        if x!= y and x != z and y != z:
            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 = 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)

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

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

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

cnt = 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]:
			cnt -= 1


print(cnt)

Solution (Python):
from collections import Counter
N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')

for i in range(N):
    for j in range(1,N):
        if (i-j<0 or i+j>=N):
            break
        l = S[i-j]
        m = S[i]
        r = S[i+j]
        if l!=m and m!=r and r!=l:
            ans -= 1
print (ans)

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



n=S.count("R")*S.count("G")*S.count("B")
count=0
for l in range(N-2):
    for r in range(l+2,N):
        m=(l+r)/2
        if m%1.0!=0:
            continue
        m=int(m)
        tmp =[S[l], S[r], S[m]]
        if len(set(tmp))==3:
            count+=1
            
            

print(n-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 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())
sentense = input()
count_1 = sentense.count("R")*sentense.count("G")*sentense.count("B")
count_1_and_not_2 = 0
for i in range(N):
    for j in range(i+1, N):
        k = (j-i)+j
        if k >= N:
            break
        if sentense[i] != sentense[j] and sentense[j] != sentense[k] and sentense[k] != sentense[i]:
            count_1_and_not_2 += 1

print(count_1-count_1_and_not_2)

Solution (Python):
def main():
    import sys
    from collections import Counter
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    cnt = Counter(S)
    ans = cnt['R'] * cnt['G'] * cnt['B']
    for i in range(N-2):
        for j in range(i+1, N-1):
            k = j + j - i
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
length = int(input())
text = input()

r_count = text.count("R")
g_count = text.count("G")
b_count = text.count("B")

answer = r_count * g_count * b_count

for i in range(length-2):
    for j in range(i+1, length-1):
        k = i + (j-i)*2
        if k < length and text[i]!=text[j] and text[i]!=text[k] and text[j]!=text[k]:
            answer -= 1
        
print(answer)

Solution (Python):
N = int(input())
S = input()
count = 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
    for j in range(i+1,N):
        if 2*j-i <= N-1:
            if S[i] != S[j] and S[j]!=S[2*j-i] and S[2*j-i]!=S[i]:
                count -= 1
count += r*g*b
print(count)

Solution (Python):
N = int(input())
S = input()
Rnum = 0
Gnum = 0
Bnum = 0
for i in S:
    if i == 'R':
        Rnum += 1
    elif i == 'G':
        Gnum += 1
    else:
        Bnum += 1

ans = Rnum*Gnum*Bnum

for i in range(1, N//2 + 1):
    for j in range(N):
        if j + 2*i >= N:
            break
        else:
            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)

Solution (Python):
N = int(input())
S = input()
Rnum = 0
Gnum = 0
Bnum = 0
for i in S:
    if i == 'R':
        Rnum += 1
    elif i == 'G':
        Gnum += 1
    else:
        Bnum += 1

ans = Rnum*Gnum*Bnum

for i in range(1, N//2 + 1):
    for j in range(N):
        if j + 2*i >= N:
            break
        else:
            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)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N=int(input())
    S=str(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)
    lr=len(R)
    lg=len(G)
    ans=lr*lg*(N-lr-lg)

    cnt=0
    for i in range(N):
        for j in range(i+1,N):
            k=2*j-i
            if k>=N:
                continue
            else:
                if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                    cnt+=1
    print(ans-cnt)




resolve()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
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)



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

count = 0

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

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

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



for i in range(N-2):
    for j in range(i+1, N-1):
        k = 2*j - i
        if k < N:
            if (S[i] != S[j]) & (S[j] != S[k]) & (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 j in range(n):
  J=s[j]
  for i in range(j):
    k=2*j-i
    if k<n:
      I=s[i]
      K=s[k]
      if I!=J!=K!=I:
        ans-=1   
print(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)
r=len(R)
g=len(G)
b=len(B)

c=0
for i in range(n):
	for j in range(i+1,n):
		k=2*j-i
		if k<n:
			if len(set([S[i],S[j],S[k]]))==3:
				c+=1

print(r*g*b-c)

Solution (Python):
N = int(input())
str = input()
S = []
for i in range(N):
    if str[i] == 'R':
        S.append(0)
    elif str[i] == 'G':
        S.append(1)
    else:
        S.append(2)

ans = 0
for i in range(N):
    left = [0, 0, 0]
    cent = [0, 0, 0]
    right = [0, 0, 0]
    for j in range(N):
        if j < i:
            left[S[j]] += 1
        elif i == j:
            cent[S[j]] += 1
        else:
            right[S[j]] += 1
    for a in range(3):
        for b in range(3):
            for c in range(3):
                if a == b or a == c or b == c:
                    continue
                ans += left[a] * cent[b] * right[c]
    for d in range(N):
        left = i - d
        right = i + d
        if left < 0 or right >= N or S[left] == S[i] or S[left] == S[right] or S[i] == S[right]:
            continue
        ans -= 1
print(ans)

Solution (Python):



import sys


from bisect import bisect_left

N = int(input())
S = input()
R = []
G = []
B = []

cnt_r, cnt_g, cnt_b = 0, 0, 0
for idx, s in enumerate(S):
    if s == 'R':
        R.append(idx+1)
        cnt_r += 1
    if s == 'G':
        G.append(idx+1)
        cnt_g += 1
    if s == 'B':
        B.append(idx+1)
        cnt_b += 1


if (cnt_r > 0) and (cnt_g > 0) and (cnt_b > 0):
    pass
else:
    print(0)
    sys.exit()

R0, G0, B0 = R, G, B

sum = 0

for i in range(6):
    if i == 0:
        R, G, B, temp = R0, G0, B0, 'B'
    elif i == 1:
        R, G, B, temp = R0, B0, G0, 'G'
    elif i == 2:
        R, G, B, temp = B0, R0, G0, 'G'
    elif i == 3:
        R, G, B, temp = B0, G0, R0, 'R'
    elif i == 4:
        R, G, B, temp = G0, R0, B0, 'B'
    elif i == 5:
        R, G, B, temp = G0, B0, R0, 'R'

    for s in R:
        if s > G[-1]:
            break
        idx1 = bisect_left(G, s)
        for g in G[idx1:]:
            if g > B[-1]:
                break
            idx2 = bisect_left(B, g)
            sum += len(B[idx2:])
            if 2 * g - s <= N:
                if S[2 * g - s - 1] == temp:
                    sum -= 1

print(sum)

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

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

count=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[j] !=S[k] and S[k] != S[i]:
      count -=1

print(count)

Solution (Python):
from itertools import permutations

N = int(input())

R = set()
G = set()
B = set()
for i, s in enumerate(input(), start=1):
    if s == 'R':
        R.add(i)
    if s == 'G':
        G.add(i)
    if s == 'B':
        B.add(i)

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

def calc(A, B, C):
    ret = 0
    for a in A:
        for b in B:
            c = 2 * a - b
            if c in C:
                ret += 1
    return ret

ans -= calc(R, G, B)
ans -= calc(G, B, R)
ans -= calc(B, R, G)

print(ans)

Solution (Python):


int1 = lambda x: int(x) - 1
def II(): return int(input())

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

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n = II()
    S = list(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)

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

    print(len(R) * len(G) * len(B) - cnt)

if __name__ == '__main__':
    solve()

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)
    elif S[i] == 'B':
        B.append(i)
ans = len(R)*len(G)*len(B)
for i in range(1,(N+1)//2):
    for j in range(i,N-i):
        if S[j] != S[j-i] and S[j] != S[j+i] and S[j-i] != S[j+i]:
            ans -= 1
print(ans)

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

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

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

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

count=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[j] !=S[k] and S[k] != S[i]:
      count -=1

print(count)

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

r=0
g=0
b=0

for i in range(n):
    if s[i]=='R':
        r+=1
    if s[i]=='G':
        g+=1
    if s[i]=='B':
        b+=1
ans=r*g*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 = str(input())
r = 0
g = 0
b = 0

for i in range(N):
  if S[i] == 'R':
    r += 1
  elif S[i] == 'G':
    g += 1
  else:
    b += 1
tot = r*g*b

minus = 0
for i in range(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]:
        minus += 1
print(tot - minus)

Solution (Python):
from collections import Counter
N = int(input())
S = str(input())
S += "x"*4001
c = Counter(S)
after_happen = {}
after_happen[0] = c.copy()
after_happen[0][S[0]] -= 1

for i in range(1, N):
    after_happen[i] = after_happen[i-1].copy()
    after_happen[i][S[i]] -= 1

ans = 0
rgb = set(["R", "G", "B"])
for i in range(N-2):
    for j in range(i+1, N-1):
        
        if S[i] == S[j]:
            continue
        
        target = rgb - set([S[i], S[j]])
        target = list(target)[0]
        ans += after_happen[j][target]
        if S[j+(j-i)] == target:
            ans -= 1
print(ans)

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

r = 0
g = 0
b = 0

for c in s:
    if c == "R":
        r += 1
    elif c == "G":
        g += 1
    else:
        b += 1

all_patterns = r * g * b
sub = 0

for i in range(n):
    for j in range(i+1, n):
        if s[i] == s[j]: continue
        k = j * 2 - i
        if k >= n or s[k] == s[i] or s[k] == s[j]: continue
        sub += 1
print(all_patterns-sub)

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)


ans = 0

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

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

Solution (Python):

import sys
from collections import Counter
from itertools import permutations, combinations
from math import ceil, floor, gcd
sys.setrecursionlimit(10 ** 6)

def input():
    return sys.stdin.readline()[:-1]

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

    c = Counter(S)
    r_c = c['R']
    g_c = c['G']
    b_c = c['B']
    ans = r_c * g_c * b_c

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

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

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

counter = Counter(S)
r_count = counter['R']
g_count = counter['G']
b_count = counter['B']

ans = r_count * g_count * b_count
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k > N-1:
            break
        if i < j < k and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)