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



Sample input:
4
RRGB


Sample output:
1

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    S = list(input())
    ans = S.count('R') * S.count('G') * S.count('B')
    for i in range(N-2):
        for j in range(i+1, N + (-(N-i-1)//2)):
            k = j+(j-i)
            if len(set([S[i], S[j], S[k]])) == 3:
                ans-=1
    return ans
print(solve())

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
S = input()

cnt = defaultdict(int)
for i in range(N):
    s = S[i]
    cnt[s] += 1

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

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[i] != S[2 * j - i] and S[j] != S[2 * j - i]:
                ans -= 1
        else:
            break

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N-2):
    for j in range(i+1, N + (-(N-i-1)//2)):
        k = j+(j-i)
        if len(set([S[i], S[j], S[k]])) == 3:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
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 + (-(N-i-1)//2)):
        k = j+(j-i)
        if len(set([S[i], S[j], S[k]])) == 3:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r_count = s.count('R')
g_count = s.count('G')
b_count = s.count('B')

count=0
for i in range(n):
    a = s[i]
    for j in range(i+1,n):
        if (2*j-i)>(n-1):
            break
        b=s[j]
        c=s[2*j-i]
        if a!=b and b!=c and a!=c:
            count+=1
print(r_count*g_count*b_count-count)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r_count = s.count('R')
g_count = s.count('G')
b_count = s.count('B')

count=0
for i in range(n):
    a = s[i]
    for j in range(i+1,n):
        if (2*j-i)>(n-1):
            continue
        b=s[j]
        c=s[2*j-i]
        if a!=b and b!=c and a!=c:
            count+=1
print(r_count*g_count*b_count-count)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')

cnt = r*g*b

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

Submission status: Accepted

Solution code:
from collections import Counter
from sys import exit

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

l=list(Counter(s).values())
if len(l)<3:
	print("0")
	exit()
	
res=l[0]*l[1]*l[2]

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

Submission status: Accepted

Solution code:
def solve2():
    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):
        for j in range(i+1,n):
            k = j*2-i
            if k >= n:
                break
            elif s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt +=1
    return r*g*b - cnt

print(solve2())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    N = int(input())
    S = input()

    # (1) Si≠Sj & Sj≠Sk & Sk≠Si ← すべて違う文字(RGBを網羅している)ということ
    # (2) j-i≠k-j ← 等間隔でないということ 

    R = 0
    G = 0
    B = 0
    for s in S:
        if s == "R":
            R += 1
        elif s == "G":
            G += 1
        else:
            B += 1    
    total = R*G*B

    # 条件に合わない件数をカウント
    for i in range(N-2):
        for j in range(i+1, N-1):
            # 文字の位置が等間隔の場合をチェック
            k = j + (j-i)
            # カウントしないケースは以下の通り
            # (1) チェック対象の3文字目の位置がNを超える
            # (2) 同じ文字が含まれる場合 ← すでに除外済み
            if k >= N or S[i] == S[j] or S[i] == S[k] or S[j] == S[k]:
                continue
            
            total -= 1
    
    print(total)

Submission status: Accepted

Solution code:
# D - RGB Triplets

N = int(input())
S = str(input())
R_cnt = S.count('R')
G_cnt = S.count('G')
B_cnt = S.count('B')
total = R_cnt * G_cnt * B_cnt

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

print(total - cnt)

Submission status: Accepted

Solution code:
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(n-2*i):
        if {s[j], s[j+i], s[j+2*i]} == {"R", "G", "B"}:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
def main():
    ans = s.count('R') * s.count('G') * s.count('B')

    if(n<4):
        return 0
    
    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] or s[i]== s[k] or s[j]==s[k]):
                continue
            ans -= 1
    return ans
print(main())

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
r = S.count('R')
g = S.count('G')
b = S.count('B')
 
counter = 0
for i in range(N):
    for j in range(i+1, N):
        k = 2 *j - i
        if k >= N:
            break
        elif S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            counter += 1
 
print(r * g * b - counter)

Submission status: Accepted

Solution code:
#! /usr/bin/env python3

from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

import copy
N = int(input())
S = input()

# ct = 0

# def dfs(index, RGB, indexs):
#     global ct
#     if RGB[S[index]] == 1:
#         return
#     else:
#         RGB[S[index]] = 1
#         indexs += [index]
        
#         if len(indexs) == 2:

#             for key, value in RGB.items():
#                 if value == 0:
#                     ct += Counter(S[index+1:])[key]
#                     h = indexs[1]+indexs[1] - indexs[0] +1
#                     if 0 < h <= N and S[h-1] == key:
#                         ct -= 1

#                     return

#         for i in range(index+1, N):
#             dfs(i, RGB.copy(), copy.copy(indexs))

# for i in range(N):
#     dfs(i, {"R":0, "G":0, "B":0}, [])

# print(ct)

c = Counter(S)
al = c["R"] * c["G"] * c["B"]

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf
# @pysnooper.snoop()
#import pysnooper # debug

import math
#from sortedcontainers import SortedList, SortedDict, SortedSet # no in atcoder
import bisect

class Solver():

    R = 'R'
    G = 'G'
    B = 'B'

    def __init__(self, n, s):
        self.n = n
        self.s = s
        self.rgb = self.make_rgb()
        self.large = self.calc_large()
        #print('self.large') # debug
        #print(self.large) # debug
        self.trident = 0

    def make_rgb(self):
        rgb = {
                self.R: [],
                self.G: [],
                self.B: [],
                }
        for i, c in enumerate(self.s):
            rgb[c].append(i)
        return rgb

    def calc_large(self):
        r = self.rgb[self.R]
        g = self.rgb[self.G]
        b = self.rgb[self.B]
        l = [
                (self.R, len(r)),
                (self.G, len(g)),
                (self.B, len(b)),
                ]
        l = sorted(l, key=lambda tpl: tpl[1])
        #print('l') # debug
        #print(l) # debug
        return l[-1][0]

    def get_smalls(self):
        if self.large == self.R:
            return self.rgb[self.G], self.rgb[self.B]
        if self.large == self.G:
            return self.rgb[self.R], self.rgb[self.B]
        if self.large == self.B:
            return self.rgb[self.G], self.rgb[self.R]

    def run(self):
        s1, s2 = self.get_smalls()
        for i1 in s1:
            for i2 in s2:
                self.check_3way(i1, i2)
        return self.calc_ans()

    def check_3way(self, i1, i2):
        if not i1 < i2:
            i1, i2 = i2, i1
        d = i2 - i1
        self.hit_then_count(i1 - d)
        self.hit_then_count(i1 + d / 2)
        self.hit_then_count(i2 + d)

    def hit_then_count(self, key):
        if int(key) != key:
            return
        if not (0 <= key < len(self.s)):
            return
        key = int(key)
        if self.s[key] == self.large:
            self.trident += 1

    def calc_ans(self):
        r = len(self.rgb[self.R])
        g = len(self.rgb[self.G])
        b = len(self.rgb[self.B])
        return r * g * b - self.trident




if __name__ == '__main__':
    n = int(input())
    s = input()
    ans = Solver(n, s).run()
    #print('ans') # debug
    print(ans)

    #print('\33[32m' + 'end' + '\033[0m') # debug

Submission status: Accepted

Solution code:
N = int(input())
List = list(input())
ans = (List.count("R"))*(List.count("G"))*(List.count("B"))

for i in range(1,2000):
  for j in range(N-i*2):
    if {List[j], List[j+i], List[j+2*i]} == {"R","G","B"}:
      ans -=1
print(ans)

Submission status: Accepted

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

r = len([i for i in range(n) if s[i] == "R"])
g = len([i for i in range(n) if s[i] == "G"])
b = len([i for i in range(n) if s[i] == "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[k] != s[i]:
            count += 1

print(r * g * b - count)

Submission status: Accepted

Solution code:
n=int(input())
S=input()
R={}
G={}
B={}
r=0
g=0
b=0
for i in range(n):
    if S[i] == 'R':
        R[i] = None
    elif S[i] == 'G':
        G[i] = None
    else:
        B[i] = None
cnt=len(R)*len(G)*len(B)
for b in B:
    for g in G:
        if (b+g)/2 in R:
            cnt-=1
for b in B:
    for r in R:
        if (b+r)/2 in G:
            cnt-=1
for r in R:
    for g in G:
        if (r+g)/2 in B:
            cnt-=1
print(cnt)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
S = list(input())
 
r = S.count("R")
g = S.count("G")
b = S.count("B")
 
 
A = list(range(len(S)))
cnt = 0
for x, y in combinations(A, 2):
	if (x+y)//2 == (x+y)/2:
		z = (x+y)//2
		if S[x] != S[y] and S[x] != S[z] and S[y] != S[z]:
			cnt += 1
 
print(r*g*b-cnt)

Submission status: Accepted

Solution code:
from collections import Counter
from itertools import combinations, product
n = int(input())
s = input()
ct = {i : [] for i in "RGB"}
for i, _s in enumerate(s):
    ct[_s].append(i)

cnt = 1
for v in ct.values():
    cnt *= len(v)

for combi in product(ct["R"], ct["G"]):
    _min = min(combi) - abs(combi[0] - combi[1])
    _max = max(combi) + abs(combi[0] - combi[1])
    if _min >= 0 and s[_min] == "B":
        cnt -= 1
    if _max < n and s[_max] == "B":
        cnt -= 1
    if (combi[1] - combi[0]) % 2 == 0 and s[abs(combi[0] - combi[1]) // 2 + min(combi)] == "B":
        cnt -= 1
print(cnt)

Submission status: Accepted

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

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

cnt=0

for i in range(n):
  d=0
  while 1:
    d+=1
    j=i+d
    k=j+d
    if not k<n:
      break
    if not (s[i]==s[j] or s[j]==s[k] or s[k]==s[i]):
      cnt+=1
print(r*g*b-cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

r = 0
g = 0
b = 0
for i, s in enumerate(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 S[i] == 'R':
            if S[j] == 'G':
                if 2 * j - i < N and S[j + j - i] == 'B':
                    ans -= 1
            if S[j] == 'B':
                if 2 * j - i < N and S[j + j - i] == 'G':
                    ans -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                if 2 * j - i < N and S[j + j - i] == 'B':
                    ans -= 1
            if S[j] == 'B':
                if 2 * j - i < N and S[j + j - i] == 'R':
                    ans -= 1
        else:
            if S[j] == 'R':
                if 2 * j - i < N and S[j + j - i] == 'G':
                    ans -= 1
            if S[j] == 'G':
                if 2 * j - i < N and S[j + j - i] == 'R':
                    ans -= 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed May  6 02:13:01 2020

@author: shinba
"""
import sys

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)
    else:
        B.append(i+1)

if R==[] or G==[] or B==[]:
    print(0)
    sys.exit()
    
ans = len(R)*len(G)*len(B)

m = min(B)
M = max(B)

for i in R:
    for j in G:
        a = 2*j - i 
        b = (i+j)//2 if (i+j)%2 == 0 else 0
        c = 2*i - j
        if m <= a <= M:
            if S[a-1] == "B":
                ans -= 1
        if b != 0 and S[b-1] == "B":
            ans -= 1
        if m <= c <= M:
            if S[c-1] == "B":
                ans -= 1
            
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = input()

R = {}
G = {}
B = {}
rc = 0
gc = 0
bc = 0

for i in range(n):
    index = i * 2
    if S[i] == 'R':
        R[index] = i
        rc += 1
    elif S[i] == 'G':
        G[index] = i
        gc += 1
    else:
        B[index] = i
        bc += 1

cnt = rc * gc * bc

for b in B:
    for g in G:
        if (B[b] + G[g]) in R:
            cnt -= 1

for r in R:
    for b in B:
        if (R[r] + B[b]) in G:
            cnt -= 1

for r in R:
    for g in G:
        if (R[r] + G[g]) in B:
            cnt -= 1

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
S = input()

R = {}
G = {}
B = {}

for i in range(n):
    if S[i] == 'R':
        R[i] = None
    elif S[i] == 'G':
        G[i] = None
    else:
        B[i] = None

#print(R, G, B)

cnt = 0

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

for b in B:
    for g in G:
        if (b + g) / 2 in R:
            cnt -= 1

for r in R:
    for b in B:
        if (r + b) / 2 in G:
            cnt -= 1

for r in R:
    for g in G:
        if (r + g) / 2 in B:
            cnt -= 1
            
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
counter = 0

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

rgb = S.count("R")*S.count("G")*S.count("B")
print(rgb-counter)

Submission status: Accepted

Solution code:
a=int(input())
s=input()
ans=s.count("R")*s.count("G")*s.count("B")
for i in range(a):
  for j in range(i+1,(a+i+1)//2):
    if s[i]!=s[j] and s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
      ans-=1
print(ans)

Submission status: Accepted

Solution code:
a=int(input())
s=input()
ans=s.count("R")*s.count("G")*s.count("B")
for i in range(a):
  for j in range(i+1,(a+i+1)//2):
    if s[i]!=s[j] and s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
      ans-=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

r = s.count('R')
g = s.count('G')
b = s.count('B')
all_count = r*g*b

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

Submission status: Accepted

Solution code:
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-i+j
        if k<N and S[k]!=S[i] and S[k]!=S[j] and S[i]!=S[j]:
            ans -= 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(A)

Submission status: Accepted

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

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

print(A)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
rr = 0
gg = 0
bb = 0
for i in range(n):
    if s[i] == "R":
        rr += 1
    if s[i] == "G":
        gg += 1
    if s[i] == "B":
        bb += 1

ans = rr * gg * bb

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

Submission status: Accepted

Solution code:
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

all = r*g*b; sub = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue
        else:
            k = 2 * j - i
            if k >= N:
                continue
            else:
                if (S[j] == S[k]) or (S[i] == S[k]):
                    continue
                else:
                    sub+=1

print(all - sub)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
r,g,b = S.count('R'),S.count('G'),S.count('B')
total = r*g*b

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

print(total-count)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
S = input()
out = S.count('R') * S.count('G') * S.count('B')

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))
for i in range(1, N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      out -= 1
print(out)

Submission status: Accepted

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

dic = {'R':0, 'G':0, 'B':0}
for i in range(N):
  dic[S[i]] += 1

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

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

      if S[i] == 'R' and S[j] == 'B' and S[2*j - i] == 'G':
        ans -= 1

      if S[i] == 'G' and S[j] == 'R' and S[2*j - i] == 'B':
        ans -= 1

      if S[i] == 'G' and S[j] == 'B' and S[2*j - i] == 'R':
        ans -= 1
      
      if S[i] == 'B' and S[j] == 'G' and S[2*j - i] == 'R':
        ans -= 1

      if S[i] == 'B' and S[j] == 'R' and S[2*j - i] == 'G':
        ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
s=list(input())
C=Counter(s)
r,g,b=C["R"],C["G"],C["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-1 and s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                ans-=1

print(ans)

Submission status: Accepted

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

r, g, b = 0, 0, 0
rr, gg, bb = [0] * n, [0] * n, [0] * n
rcs, gcs, bcs = [0] * n, [0] * n, [0] * n
for i, c in enumerate(s):
    if c == 'R':
        r = r + 1
        rr[i] = 1
    elif c == 'G':
        g = g + 1
        gg[i] = 1
    else:
        b = b + 1
        bb[i] = 1
    rcs[i], gcs[i], bcs[i] = r, g, b

ans = 0
for j in range(1, n-1):
    l = min(j, n-1-j)
    if s[j] == 'R':
        cs1, cs2, s1, s2 = gcs, bcs, gg, bb
    elif s[j] == 'G':
        cs1, cs2, s1, s2 = rcs, bcs, rr, bb
    else:
        cs1, cs2, s1, s2 = rcs, gcs, rr, gg
    c1 = cs1[j-1], cs1[n-1]-cs1[j]
    c2 = cs2[j-1], cs2[n-1]-cs2[j]
    ans = ans + c1[1] * c2[0] + c1[0] * c2[1]
    for i in range(1, l+1):
        ans = ans - s1[j-i] * s2[j+i] - s1[j+i] * s2[j-i]

print(ans)

Submission status: Accepted

Solution code:
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)
count = 0
unchi = set(b)
for i in r:
	for j in g:
		u = abs(i-j)
		count += [0,1][(max(i,j)+u) in unchi] + [0,1][(min(i,j)-u) in unchi] + [0,1][u%2 == 0 and (i+j)//2 in unchi]
print(len(r)*len(g)*len(b)-count)

Submission status: Accepted

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

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

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

ans=len(r)*len(g)*len(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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
num = 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]:
            num -=1
print(num)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = s.count('R')*s.count('G')*s.count('B')
for i in range(n):
    if s[i] == 'R':
        s[i] = 1
    elif s[i] == 'G':
        s[i] = 2
    elif s[i] == 'B':
        s[i] = 4

for i in range(n):
    for j in range(i+1,n):
        k = 2*j -i
        if k >= n:
            continue
        elif s[i]+s[j]+s[k] == 7:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    n = int(stdin.readline())
    s = stdin.readline()[:-1]

    nr = 0
    ng = 0
    nb = 0
    nn = 0

    for i in range(n):
        if s[i] == 'R':
            nr += 1
        elif s[i] == 'G':
            ng += 1
        elif s[i] == 'B':
            nb += 1
        for j in range(i + 1, n):
            k = j + j - i
            if 0 <= k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                nn += 1

    print(nr * ng * nb - nn)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import os

def file_input():
    f = open('Beginner_Contest_162/input.txt', 'r')
    sys.stdin = f

def main():
    #file_input()
    N=int(input())
    S=input()
    # print(S)
    R=S.count('R')
    G=S.count('G')
    B=S.count('B')

    sum=R*G*B
    # print(sum)
    for i in range(N-2):
        # print(i)
        for j in range(i+1,-(-(N+i)//2)):
            k=2*j-i
            # print("i:"+str(i)+" j:"+str(j)+" k:"+str(k))
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                sum-=1
                # print(S[i]+S[j]+S[k])


    print(sum)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
S=list(input())
import collections
c=collections.Counter(S)
if len(c)==3:
  
  u=1
  for i in c.values():
    u=u*i 
  for i in range(N-2):
    for j in range(i+1,int((N-1+i)/2)+1):
      
        if set([S[i],S[j],S[2*j-i]])=={'B','R','G'} :
          u=u-1
  print(u)       
else:
  print(0)

Submission status: Accepted

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

v=0
rgb=[0,0,0]
for x in range(n):
  if s[x]=="R":
    rgb[0]+=1
  elif s[x]=="G":
    rgb[1]+=1
  else:
    rgb[2]+=1
  
base=rgb[0]*rgb[1]*rgb[2]
  
for a in range(n):
  for b in range(a+1,n):
    c=2*b-a
    if c>=n:
      break
    else:
      if s[a]!=s[b] and s[a]!=s[c] and s[b]!=s[c] and b*2==c+a:
        base-=1
          
print(base)

Submission status: Accepted

Solution code:
import collections
n=int(input())
s=input()

cnt=0
def excludeRGB(c1,c2,c3):
  cnt=0
  for a in range(len(s)-2):
    if s[a] != c1:
      continue
    for b in range(1,len(s)):
      if a+b*2 >= n or a+b >= n:
        break
      if s[a+b] == c2 and s[a+b*2] == c3:
        cnt+=1
  return cnt

ans=0
def rgb(c1,c2,c3):
  ans,c1num,c2num=0,0,0
  for i in s[::-1]:
    if i == c1:
      c1num+=1
    elif i == c2:
      c2num+=c1num
    elif i == c3:
      ans+=c2num
  return ans
cnt+=excludeRGB("R","G","B")
cnt+=excludeRGB("R","B","G")
cnt+=excludeRGB("B","G","R")
cnt+=excludeRGB("B","R","G")
cnt+=excludeRGB("G","B","R")
cnt+=excludeRGB("G","R","B")

ans+=rgb("R","G","B")
ans+=rgb("R","B","G")
ans+=rgb("B","G","R")
ans+=rgb("B","R","G")
ans+=rgb("G","B","R")
ans+=rgb("G","R","B")
print(ans-cnt)

Submission status: Accepted

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

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

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

ans = len(r2) * len(g2) * len(b2)
minC = r2
minC2 = g2
maxC = b

for i in minC:
  for j in minC2:
    id = max(i,j) + abs(i-j)
    if id <= n and maxC[id] == 1:
      ans -= 1
    id = min(i,j) - abs(i-j)
    if id >= 1 and maxC[id] == 1:
      ans -= 1
    id = i+j
    if id%2 == 0 and maxC[id//2] == 1:
      ans -= 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
#n,k=map(int,input().split())
#pl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

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:
            continue
        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
            count+=1

print(r*g*b-count)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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):
  for j in range(i+1, n-1):
    k = 2*j-i
    
    if k >= n or s[i] == s[j] or s[j] == s[k] or s[i] == s[k]:
      continue
      
    rgb -= 1
    
print(rgb)

Submission status: Accepted

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

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

one = r*g*b
not_two = 0

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

ans = one - not_two
print(str(ans))

Submission status: Accepted

Solution code:
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-1:
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                ans -= 1

print(ans)