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()
c = [[0 for i in range(3)] for i in range(len(s))]
color = ['R','G','B']

for i in range(3):
	c[0][i] = (s[0]==color[i])

for i in range(1, len(s)):
	for j in range(3):
		if s[i]==color[j]:
			c[i][j] = c[i-1][j]+1
		else:
			c[i][j] = c[i-1][j]

res = 0
for i in range(len(s)-2):
	for j in range(i+1, len(s)-1):
		if s[i] != s[j]:
			for k in range(3):
				if s[i] != color[k] and s[j]!= color[k]:
					
					res += c[n-1][k] - c[j][k]

					if 2*j-i<n:
						
						res += -c[2*j-i][k] + c[2*j-i-1][k]

print(res)

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

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

rgb = r*g*b

not_j2_num = 0

for i in range(len(s)-2):
    for j in range(i+1,len(s)-1):
        
        k=j+j-i
        
        
        
        if k<len(s) and len(set([s[i],s[j],s[k]]))==3:
            
            not_j2_num += 1
            
print(rgb - not_j2_num)

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

if __name__ == "__main__":
    main()

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

if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter as coun
from itertools import combinations as comb

import sys
input = sys.stdin.readline

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

C = coun(S)
ans = C["R"] * C["G"] * C["B"]
for i, j in comb(range(N), 2):
  k = 2 * j - i
  if k < N:
    A, B, C = S[i], S[j], S[k]
    if A != B and B != C and C != A: ans -= 1

print(ans)

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

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):
    k = 2 * j - i
    if k < N:
      A, B, C = S[i], S[j], S[k]
      if A != B and B != C and C != A: ans -= 1

print(ans)

Solution (Python):

"""
Created on Tue Sep  8 01:23:04 2020

@author: liang
"""

N = int(input())
S = input()
R_list = list()
G_list = list()
B_list = list()

for i in range(N):
    if S[i] == 'R':
        R_list.append(i)
    elif S[i] == 'G':
        G_list.append(i)
    else:
        B_list.append(i)

ans = len(R_list)*len(G_list)*len(B_list)
"""
set1 = set()
for r in R_list:
    for g in G_list:
        
        if r > g:
           
            r, g = g, r 
           
            【rは絶対書き換えたらダメ】
            gのfor文中 rの値は固定して考えているので、
            外側ループの変数を書き換えるような演算をしてはいけない
              => for文内で別の変数（コピー）を用意し、そのコピーについて演算を行うこと
           
            
        if 2*g - r < N and S[2*g-r] == 'B':
            set1.add((r,g))
            ans -= 1
        if 2*r - g >= 0 and S[2*r-g] == 'B':
            set1.add((r,g))
            ans -= 1
        if (r+g)%2 == 0 and S[(r+g)//2] =='B':
            set1.add((r,g))
            ans -= 1   

"""
for r in R_list:
    for g in G_list:
        up = max(r,g)
        down = min(r,g)
        diff = up - down
        if up + diff < N and S[up+diff] == 'B':
            ans -= 1
        if down - diff >= 0 and S[down - diff] == 'B':
            ans -= 1
        if diff%2 == 0 and S[down + diff//2] =='B':
            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+1)
    elif S[i] == 'G':
        G.append(i+1)
    elif S[i] == 'B':
        B.append(i+1)

lenb = len(B)

cnt = 0
for r in R:
    for g in G:
        up = max(r, g)
        down = min(r, g)
        diff = up - down
        
        chk = 0
        if up + diff <= N:
            if S[up+diff-1] == 'B':
                chk += 1
        if down-diff >= 1:
            if S[down-diff-1] == 'B':
                chk += 1
        if diff%2 == 0:
            if S[int(up-diff/2-1)] == 'B':
                chk += 1
        cnt += lenb - chk

print(cnt)

Solution (Python):
import sys
from math import ceil as C, floor as F, sqrt, gcd as G
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
import heapq as HQ
from operator import mul

class Heap:
  def __init__(self, data, reverse=False):
    self.reverse = -1 if reverse else 1
    self.data = [self.reverse * d for d in data]
    HQ.heapify(self.data)
  def push(self, x): return HQ.heappush(self.data, self.reverse * x)
  def pop(self): return self.reverse * HQ.heappop(self.data) 

ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def prod(list): return R(mul, list, 1)
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

n = I()
rgb = S()

cnt = CNT(list(rgb))
ans = prod([cnt[c] for c in ['R', 'G', 'B']])

for i, r in enumerate(rgb):
    for j, g in enumerate(rgb[i+1:], start=i+1):
        k = j + (j - i)
        if k < len(rgb):
            if r != g and g != rgb[k] and r != rgb[k]:
                ans -= 1

print(ans)

Solution (Python):
'''
Created on 2020/09/07

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write

  N=int(pin())
  S=pin()[:-1]
  r=0
  g=0
  b=0
  cnt=0
  for i in range(N):
    if S[i]=="R":
      r+=1
    elif S[i]=="G":
      g+=1
    elif S[i]=="B":
      b+=1
    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+=1
  print(r*g*b-cnt)
  return 
main()

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
from numba import njit
def main():
    n = int(input())
    s = np.array(list(read().rstrip()))

    r = np.sum(s == 'R')
    g = np.sum(s == 'G')
    b = np.sum(s == 'B')
    @njit
    def f(s):
        ret = 0
        for i1 in range(n):
            for i2 in range(i1 + 1, n):
                i3 = i2 * 2 - i1
                if i3 <= n - 1:
                    if s[i1] != s[i2] and s[i1] != s[i3] and s[i2] != s[i3]:
                        ret += 1
        return ret

    res = r * g * b - f(s)
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()

    res = s.count('R') * s.count('G') * s.count('B')
    if res == 0:
        print(0)
        sys.exit()
    for i1 in range(n):
        for i2 in range(i1+1, n):
            i3 = i2 * 2 - i1
            if i3 <= n - 1:
                if s[i1] != s[i2] and s[i1] != s[i3] and s[i2] != s[i3]:
                    res -= 1
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()

    res = s.count('R') * s.count('G') * s.count('B')
    if res == 0:
        print(0)
        sys.exit()
    for i1 in range(n):
        for i2 in range(i1+1, n):
            i3 = i2 * 2 - i1
            if i3 <= n - 1:
                res -= s[i1] != s[i2] and s[i1] != s[i3] and s[i2] != s[i3]
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()

    res = s.count('R') * s.count('G') * s.count('B')
    if res == 0:
        print(0)
        sys.exit()
    for i1 in range(n):
        for i2 in range(i1+1, n):
            if i2 * 2 - i1 <= n - 1:
                if s[i1] != s[i2]:
                    res -= s[i1] != s[i2*2 - i1] and s[i2] != s[i2*2 - i1]
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    s = S()
    rgb = ["R","G","B"]
    rgb_st = set(rgb)
    rgb_acc = [[0]*n for _ in range(3)]
    index = rgb.index(s[0])
    rgb_acc[index][0] += 1
    ans = 0

    for i in range(1,n):
        for j in range(3):
            rgb_acc[j][i] = rgb_acc[j][i-1]
        index = rgb.index(s[i])
        rgb_acc[index][i] += 1
    
    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i] != s[j]:
                ex = (rgb_st-{s[i],s[j]}).pop()
                index = rgb.index(ex)
                cnt = rgb_acc[index][-1] - rgb_acc[index][j]
                if j<2*j-i<n:
                    if s[2*j-i] == ex:
                        cnt -= 1
                ans += cnt
    print(ans)
main()

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    s = S()
    rgb = [None for _ in range(n)]
    rgb_acc = [None for _ in range(n)]
    rgb_st = {"R","G","B"}
    rgb_lst = ["R","G","B"]
    for i, char in enumerate(s):
        if char == "R":
            rgb[i] = [1,0,0]
        elif char == "G":
            rgb[i] = [0,1,0]
        else:
            rgb[i] = [0,0,1]
    
    rgb_acc[0] = rgb[0]
    for i in range(1, n):
        rgb_acc[i] = rgb_acc[i-1][:]
        index = 0 if s[i]=="R" else 1 if s[i]=="G" else 2
        rgb_acc[i][index] += 1
    
    ans = 0
    for i in range(n-2):
        for j in range(i+1,n-1):
           if s[i]!=s[j]:
                ex = rgb_st - {s[i], s[j]}
                ex = ex.pop()
                index = 0 if ex=="R" else 1 if ex=="G" else 2
                cnt = rgb_acc[n-1][index]-rgb_acc[j][index]
                if j+1 <= 2*j-i < n:
                    if s[2*j-i] == ex:
                        cnt -= 1
                ans += cnt

    print(ans)
main()

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    s = S()
    rgb = [None for _ in range(n)]
    rgb_acc = [None for _ in range(n)]
    rgb_st = {"R","G","B"}
    rgb_lst = ["R","G","B"]
    for i, char in enumerate(s):
        if char == "R":
            rgb[i] = [1,0,0]
        elif char == "G":
            rgb[i] = [0,1,0]
        else:
            rgb[i] = [0,0,1]
    
    rgb_acc[0] = rgb[0]
    for i in range(1, n):
        rgb_acc[i] = rgb_acc[i-1][:]
        rgb_acc[i][rgb[i].index(1)] += 1
    
    ans = 0
    for i in range(n-2):
        for j in range(i+1,n-1):
           if s[i]!=s[j]:
                ex = rgb_st - {s[i], s[j]}
                ex = ex.pop()
                index = rgb_lst.index(ex)
                cnt = rgb_acc[n-1][index]-rgb_acc[j][index]
                if j+1 <= 2*j-i < n:
                    if s[2*j-i] == ex:
                        cnt -= 1
                ans += cnt

    print(ans)
main()

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

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

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

Solution (Python):
import math
import sys
import os
from operator import mul
import bisect

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N = I()
S = list(_S())
ans = 0

r = []
g = []
b = []

for i,s in enumerate(S):
    if s == 'R':
        r.append(i)
    if s == 'G':
        g.append(i)
    if s == 'B':
        b.append(i)













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

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

print(ans)

Solution (Python):
import math
import sys
import os
from operator import mul
import bisect

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N = I()
S = list(_S())
ans = 0

r = []
g = []
b = []

for i,s in enumerate(S):
    if s == 'R':
        r.append(i)
    if s == 'G':
        g.append(i)
    if s == 'B':
        b.append(i)













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

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

print(ans)

Solution (Python):
def main():
	N = int(input())
	S = list(input())
	R = []
	G = []
	B = []
	cnt = 0
	for i in range(len(S)):
		if S[i] == "R":
			R.append(i)
		elif S[i] == "G":
			G.append(i)
		elif S[i] == "B":
			B.append(i)
	l = len(B)
	for i in range(len(R)):
		r = R[i]
		for j in range(len(G)):
			g = G[j]
			cnt += l
			if g > r:
				M = g
				m = r
			elif r > g:
				M = r
				m = g
			d = M - m
			if (M + d) < N and S[M + d] == "B":
				cnt -= 1
			if (m - d) >= 0 and S[m - d] == "B":
				cnt -= 1
			if (M + m) % 2 == 0 and S[int((M + m) / 2)] == "B":
				cnt -= 1
	print(cnt)

main()

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):
        k = 2*j-i
        if k < n:
            if s[k] != s[i] and s[k] != s[j] and s[i] != s[j]:
                ans -= 1
        else:
            break
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
S = input()
D = Counter(S)
ans = 1
for d in D.values():
    ans *= d

if len(D.values()) < 3:
    ans = 0

for i in range(N):
    for x in range(1, (N-i+1)//2):
        if S[i] != S[i+x] and S[i+x] != S[i+2*x] and S[i+2*x] != S[i]:
            ans -= 1
print(ans)

Solution (Python):
N,S=int(input()),input()
r,g,b=0,0,0
for i in list(S):
    if i=='R':r+=1
    if i=='G':g+=1
    if i=='B':b+=1
ans=r*g*b
for i in range(N-2):
    for j in range(i+1,N-1):
        if j-i>N-j-1:break
        if 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 = input()
r = 0
g = 0
b = 0
for x in s:
  if x == 'R':
    r += 1
  elif x == 'G':
    g += 1
  else:
    b += 1
all = r*g*b
ex = 0
i = 0
for i in range(n-2):
  for j in range(i+1,(n+i-1)//2+1):
    if s[i] != s[j] and s[j] != s[2*j-i] and s[2*j-i] != s[i]:
      ex += 1
      
print(all-ex)

Solution (Python):
def main():
    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 j - i > n - j - 1:
          break
        if s[i] != s[j] and s[j] != s[2*j-i] and s[2*j-i] != s[i]:
          ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

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


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

for i in range(n) :
    a=s[i]
    for j in range(i+1,n) :
        b=s[j]
        if 2*j-i>n-1 :
            break
        c=s[2*j-i]
        if (a!=b) and (b!=c) and (c!=a) :
            ans-=1

print(ans)

Solution (Python):
def main():
    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
    ans = r*g*b - cnt
    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(1,N-1):
  for j in range(i+1,N):
    k = 2 * j - i
    if k > N:
      break
    if S[i-1] != S[j-1] and S[j-1] != S[k-1] and S[k-1] != S[i-1]:
      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):
  for j in range(i+1, n-1):
    if j - i > n - j - 1:
      break
    if s[i] != s[j] and s[j] != s[2*j-i] and s[2*j-i] != s[i]:
      ans -= 1
print(ans)

Solution (Python):
from collections import Counter
def main():
    n=int(input())
    s=input()
    d=Counter(s)
    a=d["R"]*d["G"]*d["B"]
    for i in range(n):
        t=min(n-i-1, i)
        for j in range(t+1):
            if s[i-j] != s[i] and s[i-j] != s[i+j] and s[i] != s[i+j]:
                a-=1
    print(a)
    
if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=str(input())
if n<=2:
    print(0)
    exit()
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):
        if 2*j-i>n-1:
            break
        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):
N = int(input())
S = input()
Rc = 0
Gc = 0
Bc = 0
for i in range(N):
    if S[i] == 'R':
         Rc +=1
    elif S[i] == 'G':
         Gc+=1
    elif S[i] == 'B':
         Bc+=1
c = Rc*Gc*Bc

for i in range(N-2):
     for j in range(i+1,N-1):
          if S[i] == S[j]:
               continue
          if j + j - i >= N:
               continue
          if S[i] == S[j+j-i]:
               continue
          if S[j] == S[j+j-i]:
               continue
          c -= 1
print(c)

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

ms = (N-3)//2
t = 0
for i in range(ms + 1):
    for j in range(N - (2*i + 3) + 1):
        if S[j] != S[j + i + 1] and S[j] != S[j + 2*i + 2] and S[j + i + 1] != S[j + 2*i + 2]:
            t += 1

print(r * g * b - t)

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-1:
            break
        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):
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):
        if 2*j-i>n-1:
            break
        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):
n = int(input())
s = list(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):
        if (j - i) + j < n:
            k = (j - i) + j
            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 = list(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):
        if (j - i) + j < n:
            k = (j - i) + j
            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')
cnt=0
for i in range(1,n+1):
    for j in range(i,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]:
            cnt+=1
print(r*g*b-cnt)

Solution (Python):


import sys



def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    S = input()
    r = set()
    g = set()
    b = set()
    for i in range(n):
        if S[i] == "R":
            r.add(i)
        elif S[i] == "G":
            g.add(i)
        else:
            b.add(i)
    cnt = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            s1 = S[i]
            s2 = S[j]
            k = j + (j - i)
            if k > n - 1:
                break
            s3 = S[k]
            if s1 != s2 and s2 != s3 and s3 != s1:
                cnt += 1
    ans = (len(r) * len(g) * len(b)) - cnt
    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):
def abc162d_rgb_triplets():
    n = int(input())
    s = input()
    r_cnt = 0
    g_cnt = 0
    b_cnt = 0
    for i in range(n - 1, -1, -1):
        if s[i] == 'R':
            r_cnt += 1
        elif s[i] == 'G':
            g_cnt += 1
        else:
            b_cnt += 1

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



abc162d_rgb_triplets()

Solution (Python):
n = int(input())
s = input()
r, g, b = [], [], []
cnt = 1
for i in s:
    if i == 'R':
        r.append(cnt)
    elif i == 'G':
        g.append(cnt)
    else:
        b.append(cnt)
    cnt += 1
rd = set(map(lambda x: x * 2, r)) 
gd = set(map(lambda x: x * 2, g))
bd = set(map(lambda x: x * 2, b))
ddc = 0
for i in r:
    for j in g:
        if i + j in bd:
            ddc += 1
for j in g:
    for k in b:
        if j + k in rd:
            ddc += 1
for i in r:
    for k in b:
        if i + k in gd:
            ddc += 1
print(len(r) * len(g) * len(b) - ddc)

Solution (Python):
n = int(input())
s = input()
r, g, b = [], [], []
cnt = 1
for i in s:
    if i == 'R':
        r.append(cnt)
    elif i == 'G':
        g.append(cnt)
    else:
        b.append(cnt)
    cnt += 1
rd = set(map(lambda x: x * 2, r))
gd = set(map(lambda x: x * 2, g))
bd = set(map(lambda x: x * 2, b))
ddc = 0
for i in r:
    for j in g:
        if i + j in bd:
            ddc += 1
for j in g:
    for k in b:
        if j + k in rd:
            ddc += 1
for i in r:
    for k in b:
        if i + k in gd:
            ddc += 1
print(len(r) * len(g) * len(b) - ddc)

Solution (Python):






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
























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

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

Solution (Python):

def solve(N, S):
    ans = 0
    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

    dif = 1
    while dif <= N//2+1:
        idx = 0
        while idx + dif + dif < N:
            if S[idx] != S[idx + dif] and S[idx+dif] != S[idx+2*dif] and S[idx+2*dif] != S[idx]:
                ans -= 1
            idx += 1
        dif += 1
    print(ans)
if __name__ == '__main__':
    
    
    N = int(input())
    S = input()

    solve(N, S)

Solution (Python):
N = int(input())
S = list(input())
R = []
G = []
for i in range(N):
  if S[i] == "R":
    R.append(i)
  elif S[i] == "G":
    G.append(i)
r = len(R)
g = len(G)
ans = r * g * (N - r - g)
for i in R:
  for j in G:
    a = 2 * i - j
    b = 2 * j - i
    c = i + j
    if 0 <= a < N and S[a] == "B":
      ans -= 1
    if 0 <= b < N and S[b] == "B":
      ans -= 1
    if c % 2 == 0 and S[c//2] == "B":
      ans -= 1
print(ans)

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

uns = 0
for d in range(1, (N - 1) // 2 + 1):
    for i in range(N - 2 * d):
        s, t, u = S[i], S[i + d], S[i + 2 * d]
        if s != t and t != u and u != s:
            uns += 1

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

ans = r * g * b - uns
print(ans)

Solution (Python):
def binary_search(data, value):
    left = 0            
    right = len(data) - 1            
    while left <= right:
        mid = (left + right) // 2            
        if data[mid] == value:
            
            return mid
        elif data[mid] < value:
            
            left = mid + 1
        else:
            
            right = mid - 1
    return -1            

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

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

c = 0
for i in r:
    for j in g:
        if binary_search(b, 2*i - j) != -1:
            c += 1
        if binary_search(b, 2*j - i) != -1:
            c += 1
        if binary_search(b, (i+j)/2) != -1:
            c += 1

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

Solution (Python):
N=int(input())
S=str(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 S[i]==S[j]:
      continue
    if 2*j - i < N:
      if S[2*j - i] != S[i] and S[2*j - i] != S[j]:
        ans-=1
print(ans)

Solution (Python):
N=int(input())
S=str(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 S[i]==S[j]:
      continue
    if 2*j - i < N:
      if S[2*j - i] != S[i] and S[2*j - i] != S[j]:
        ans-=1
print(ans)

Solution (Python):
import math
from math import gcd,pi
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    s = s_input()

    r_cn = s.count("R")
    g_cn = s.count("G")
    b_cn = s.count("B")

    ans = r_cn*g_cn*b_cn

    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)


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 = 2*j-i
        if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
print(ans)

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

def main():
    N = int(input())
    S = input()
    ans = 0
    r, g, b = [0 for _ in range(N+1)], [0 for _ in range(N+1)], [0 for _ in range(N+1)]
    for i in range(N):
        r[i+1], g[i+1], b[i+1] = r[i], g[i], b[i]
        if S[i] == "R":
            r[i+1] += 1
        elif S[i] == "G":
            g[i+1] += 1
        else:
            b[i+1] += 1
    
    for i in range(1, N-1):
        if S[i] == "R":
            ans += g[i] * (b[N] - b[i+1]) + b[i] * (g[N] - g[i+1])
            for j in range(1, min(i, N-i-1)+1):
                if {S[i-j], S[i+j]} == {"G", "B"}:
                    ans -= 1
        
        elif S[i] == "G":
            ans += r[i] * (b[N] - b[i+1]) + b[i] * (r[N] - r[i+1])
            for j in range(1, min(i, N-i-1)+1):
                if {S[i-j], S[i+j]} == {"R", "B"}:
                    ans -= 1
        elif S[i] == "B":
            ans += g[i] * (r[N] - r[i+1]) + r[i] * (g[N] - g[i+1])
            for j in range(1, min(i, N-i-1)+1):
                if {S[i-j], S[i+j]} == {"G", "R"}:
                    ans -= 1
    print(ans)

main()

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

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

cnt = 0
for i in range(N-3):
    if S[i] == 'R':
        r -= 1
        cnt += g * b 
        l = 1
        while (i + 2 * l < N):
            if (S[i+l] == 'G' and S[i+2*l] == 'B') or (S[i+l] == 'B' and S[i+2*l] == 'G'):
                cnt -= 1
            l += 1
    if S[i] == 'G':
        g -= 1
        cnt += r * b 
        l = 1
        while (i + 2 * l < N):
            if (S[i+l] == 'R' and S[i+2*l] == 'B') or (S[i+l] == 'B' and S[i+2*l] == 'R'):
                cnt -= 1
            l += 1
    if S[i] == 'B':
        b -= 1
        cnt += g * r
        l = 1
        while (i + 2 * l < N):
            if (S[i+l] == 'G' and S[i+2*l] == 'R') or (S[i+l] == 'R' and S[i+2*l] == 'G'):
                cnt -= 1
            l += 1
print(cnt)

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


if '__main__' == __name__:
    resolve()

Solution (Python):
def main():
    N = int(input())
    S = input()
    RGBdict = {"R":0,"G":0,"B":0}
    for i in range(N):
        RGBdict[S[i]] += 1

    if N%2 == 0:
        maxsa = (N//2) -1
    else:
        maxsa = N//2

    tmp = N - 2
    cnt = 0
    for j in range(maxsa+1):
        num = tmp-(j*2)
        for k in range( num ):
            if (S[N-num+k] != S[N-num+k-j-1]) and (S[N-num+k] != S[N-num+k-2*j-2]) and (S[N-num+k-j-1] != S[N-num+k-2*j-2]):
                cnt += 1

    ans = RGBdict["R"]*RGBdict["G"]*RGBdict["B"]
    ans -= cnt
    return ans

print(main())

Solution (Python):
n = int(input())
s = input()
r = 0
g = 0
b = 0
for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    else:
        b += 1
ans = r * g * 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
        ans -= 1
        
print(ans)