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: Time Limit Exceeded

Solution code:
from collections import Counter

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

dup=0

for width in range(2010):
    for i in range(N):
        try:
            if set((S[i],S[i+width],S[i+width*2]))==set(('R','G','B')):
                dup+=1
        except IndexError:
            break

c=Counter(S)
retval=c['R']*c['G']*c['B']-dup
print(retval)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                if s[i] == s[j] or s[i] == s[k] or s[j] == s[k] or (j - i) == k - j:
                    continue
                else:
                    count += 1
    print(count)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

dup=0

for width in range(2000):
    for i in range(N):
        try:
            if set((S[i],S[i+width],S[i+width*2]))==set(('R','G','B')):
                dup+=1
        except IndexError:
            break

c=Counter(S)
retval=c['R']*c['G']*c['B']-dup
print(retval)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

c = 0
for i in range(N-2):
    for j in range(i,N-1):
        for k in range(j,N):
            if j-i == k-j:
                pass
            else:
                if S[i] != S[j]:
                    if S[j] != S[k]:
                        if S[k] != S[i]:
                            c += 1
print (c)

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
s = ns()
d = dict()
for x in 'RGB':
    d[x] = s.count(x)
ans = 0
for i in range(n-1):
    for j in range(i):
        if s[i] != s[j]:
            t = list({'R', 'G', 'B'} - {s[i], s[j]})[0]
            ans += d[t] - ((2*i - j) < n and t == s[2*i - j])
    d[s[i]] -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(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 r in R:
    for g in G:
        #print(r, g)
        ans +=len([
            b for b in B if abs(b-r) != abs(b-g) and abs(g-b) != abs(g-r) and abs(r-b) != abs(r-g)
        ])
print(ans)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(0, N-2):
    si = S[i]

    for j in range(i+1, N-1):
        sj = S[j]
        if si == sj:
            continue
        else:
            for k in range(j+1, N):
                if si == S[k] or sj == S[k]:
                    continue
                if j - i != k - j:
                    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N = int(input())
    S = input()
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == S[j]:
                continue
            for k in range(j+1, N):
                if (j - i) == (k  - j):
                    continue
                if S[i] != S[k] and S[j] != S[k]:
                    ans += 1
    print(ans)
resolve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j+1,n):
      if s[i] != s[j] and s[j] != s[k] and s[k] != s[i] and j-i != k-j:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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

print(t)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
st = input()
i = 0
ans = 0
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if j-i != k-j:
                if st[i]!=st[j] and st[i]!=st[k] and st[j]!=st[k]:
                    ans += 1
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
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 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 = input()

nR = S.count("R")
nG = S.count("G")
nB = S.count("B")
ans = nR*nG*nB
for i in range(N):  # 開始 index
	for j in range(1, N//2+1):  # 幅
		if i+2*j >= N:
			continue
		a, b, c = S[i], S[i+j], S[i+2*j]
		if a != b and b != c and c != a:
			ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
r = []
g = []
b = []
s = input()
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 r:
    for j in g:
        for k in b:
            mn = min(i,j,k)
            mx = max(i,j,k)
            mid = (i+j+k) - (mn+mx)

            if(mx - mid != mid - mn):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
total = 0
for first in range(0, n-2):
	for second in range(first+1, n-1):
		for third in range(second+1, n):
			if third - second == second - first:
				pass
			elif s[first] == s[second] or s[first] == s[third] or s[second] == s[third]:
				pass
			else:
				total += 1
print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
ans = 0
for k in range(n):
    for j in range(k):
        for i in range(j):
            if s[i]!=s[k] and s[j]!=s[k] and s[i] !=s[j] and j-i!=k-j:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
seq = input()
ans = 0
for i, val in enumerate(seq[:-2]):
    for j, val in enumerate(seq[i+1:-1], i+1):
        for k, val in enumerate(seq[j+1:], j+1):
            if seq[i] != seq[j] and seq[i] != seq[k] and seq[j] != seq[k] and j-i != k-j:
                ans +=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
S = input()
c = Counter(S)
ans = c['R']*c['G']*c['B']
for i in range(N):
  for j in range(i+1,N):
    k = j-i+j
    if k>N-1:
      continue
    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()
assert len(S) == N
count = 0

# j-i = k-jを探す
for i in range(0, len(S)-2):
  for j in range(i+1, len(S)-1):
    k = j + (j - i)
    if k < len(S):
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        count += 1
numR = 0
numG = 0
numB = 0
for s in S:
  if s == 'R':
    numR += 1
  elif s == 'G':
    numG += 1
  else:
    numB += 1

print(numR * numG * numB - count)

Submission status: Accepted

Solution code:
from collections import Counter

def get_k(i, j):
    return 2*j - i

if __name__ == '__main__':
    N = int(input())
    S = input()
    C = Counter(S) 
    eq_cnt = 0
    cnt = C['R'] * C['G'] * C['B']
    for i in range(N):
        for j in range(i+1, N):
            k = get_k(i, j)
            if k >= N: continue
            if (S[i] != S[j] and S[i] != S[k] and S[j] != S[k]):
                eq_cnt += 1
    print(cnt - eq_cnt)

Submission status: Accepted

Solution code:

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

ris = [i for i, s in enumerate(S) if s == "R"]
gis = [i for i, s in enumerate(S) if s == "G"]
bis = [i for i, s in enumerate(S) if s == "B"]

all = len(ris) * len(gis) * len(bis)

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

ans = all - cnt


print(ans)

Submission status: Accepted

Solution code:
N=int(input())
T=input()
S=[0 for _ in range(N)]
for i in range(N):
  if T[i]=='G':
    S[i]=1
  elif T[i]=='B':
    S[i]=2
    
ls=[(0,0,0) for _ in range(N)]
for i in range(N):
  if S[i]==0:
    ls[i]=(ls[i-1][0]+1,ls[i-1][1],ls[i-1][2])
  elif S[i]==1:
    ls[i]=(ls[i-1][0],ls[i-1][1]+1,ls[i-1][2])
  else:
    ls[i]=(ls[i-1][0],ls[i-1][1],ls[i-1][2]+1)


def count(x):
  val=0
  y=(x+1)%3
  z=(x+2)%3
  for i in range(N):
    if S[i]==x:
      for j in range(i+1,N):
        if S[j]==y:
          val+=ls[N-1][z]-ls[j][z]
          if 2*j-i<N and S[2*j-i]==z:
            val-=1
        if S[j]==z:
          val+=ls[N-1][y]-ls[j][y]
          if 2*j-i<N and S[2*j-i]==y:
            val-=1
  return val

print(count(0)+count(1)+count(2))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
C = {"R", "G", "B"}
cumsum = {c:[0 for i in range(N)] for c in C}

for i in range(N):
  s = S[i]
  cumsum[s][i] = 1
for i in range(2, N+1):
  for c in C:
    cumsum[c][-i] = cumsum[c][-i] + cumsum[c][-(i-1)]
#print(cumsum)
ans = 0
for i in range(N-2):
  s = S[i]
  a, b = C - {s}
  ans += cumsum[a][i]*cumsum[b][i]

for i in range(1, (N-1)//2 + 1):
  for j in range(N-2*i):
    #print(j, j+i, j+2*i, S[j]+S[j+i]+S[j+2*i])
    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)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def inp():
    return int(input())
def inps():
    return input().rstrip()
def inpl():
    return list(map(int, input().split()))
def _debug(obj):
    print(obj, file=sys.stderr)

# import decimal
# from decimal import Decimal
# decimal.getcontext().prec = 10

# from heapq import heappush, heappop, heapify
# import math
# from math import gcd
# import itertools as it
# import collections
# from collections import deque 

# ---------------------------------------

N = inp()
S = inps()

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 = 0
for i in r:
    for j in g:
        m = i
        n = j
        if m > n:
            m, n = n, m
        sa = abs(m - n)
        ans += len(b)
        if m - sa >= 0 and S[m - sa] == "B":
            ans -= 1
        if n + sa < N and S[n + sa] == "B":
            ans -= 1
        if sa % 2 == 0 and S[m + sa//2] == "B":
            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')
cnt=r*g*b

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

print(cnt)

Submission status: Accepted

Solution code:
def check_RGB(c1, c2, c3):
    l = [c1, c2, c3]
    return ("R" in l) and ("G" in l) and ("B" in l)
    
N = int(input())
S = input()

R, G, B = 0, 0, 0
for i, s in enumerate(S):
    if s == "R":
        R += 1
    elif s == "G":
        G += 1
    else:
        B += 1

result = R * G * B

width = 1
while width * 2 < N:
    i = 0
    first = i
    mid = i + width
    last = i + width*2

    while last < N:
        if check_RGB(S[first], S[mid], S[last]):
            result -= 1

        i += 1
        first = i
        mid = i + width
        last = i + width*2

    width += 1

print(result)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r,g,b = 0,0,0
for i in s:
    if i == "R":
        r += 1
    elif i == "G":
        g += 1
    else:
        b += 1
ans_1 = r * g * b
ans_2 = 0
for i in range(n):
    for j in range(i+1,n):
        if s[i] == s[j]:
            continue
        k = j - i + j
        if k >= n or s[k] == s[i] or s[k] == s[j]:
            continue
        #print(i,j,k)
        ans_2 += 1

print(ans_1-ans_2)

Submission status: Accepted

Solution code:
# author:  Taichicchi
# created: 19.09.2020 00:14:21

import sys

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):
        try:
            k = 2 * j - i
            if (S[i] != S[j]) & (S[j] != S[k]) & (S[k] != S[i]):
                ans -= 1
        except:
            continue

print(ans)

Submission status: Accepted

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

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

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

print(r*g*b-cnt)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
cntR,cntG,cntB=S.count('R'),S.count('G'),S.count('B')

ans=cntR*cntG*cntB

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = input()
cnt = Counter(S)

ans = cnt['R']*cnt['G']*cnt['B']
#まずansに要請１を満たす(i, j, k)の組の数を入れておき，ここから，要請２を満たさないものを除いていく。

for i in range(N-2):
    for j in range(i+1, N-1):
        k = j + (j - i)
        #kは　j - i = k - j を満たす。ただし，（i, j, k）が要請１を満たしているとは限らない。
        if k < N:
            if 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, g, b = 0, 0, 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):
        k = 2 * j - i
        if k < N:
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
from collections import *

N=int(input())
S=input()
c=Counter(S)

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

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
    ans-=1

print(ans)

Submission status: Accepted

Solution code:
from collections import *

N=int(input())
S=input()
c=Counter(S)

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

for i in range(1,N//2+1):
  for j in range(i,N-i):
    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:
import bisect

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

    global ans
    ans = 0
    count(R, G, B)
    count(R, B, G)
    count(G, R, B)
    count(G, B, R)
    count(B, R, G)
    count(B, G, R)
    print(ans)


def count(I, J, K):
    global ans
    for j in J:
        i_list = I[:bisect.bisect_left(I, j)]
        k_list = set(K[bisect.bisect_right(K, j):])
        remove = len([i for i in i_list if (2 * j - i) in k_list])
        ans += len(i_list) * len(k_list) - remove


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    dict = {'R':0, 'B':0, 'G':0}
    for i in range(N):
        dict[S[i]] += 1
    ans = dict['R']*dict['B']*dict['G']
    for i in range(N-2):
        if (N-i)%2 == 0:
            tmp = int((N-i)/2)-1
        else:
            tmp = (N-i)//2
        for j in range(1,tmp+1):
            if S[i]!=S[i+j] and S[i]!=S[i+2*j] and S[i+j]!=S[i+2*j]:
                ans = ans - 1

    return ans

print(main())

Submission status: Accepted

Solution code:
from collections import Counter

n=int(input())
s=input()
c=Counter(s)

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

lim=n//2
for i in range(1,n//2+1):
  for j in range(i,n-i):
    l=s[j-i]
    m=s[j]
    r=s[j+i]
    if l!=m and m!=r and r!=l:
      ans-=1

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
from collections import Counter

n=int(input())
s=input()
c=Counter(s)

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

lim=n//2
for i in range(1,n//2+1):
  for j in range(i,n-i):
    l=s[j-i]
    m=s[j]
    r=s[j+i]
    if l!=m and m!=r and r!=l:
      ans-=1

print(ans)

Submission status: Accepted

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

from collections import Counter

c=Counter(s)

# ans = 1
# for i in list(c.values()):
#     ans*=i

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
        else:
            if 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:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N = I()
S = [''] + LS2()

R = [0]*(N+1)
G = [0]*(N+1)
B = [0]*(N+1)
for i in range(1,N+1):
    s = S[i]
    R[i] = R[i-1]
    G[i] = G[i-1]
    B[i] = B[i-1]
    if s == 'R':
        R[i] += 1
    elif s == 'G':
        G[i] += 1
    else:
        B[i] += 1

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
S = list(input())
from collections import Counter

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

for i in range(1, N):
    for j in range(N):
        first, second, third = j, j+i, j+i*2
        if third>=N:
            break
        if len(set((S[first], S[second], S[third])))==3:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
N = int(input())
S = list(input())
Sc = Counter(S)
R = Sc["R"]
G = Sc["G"]
B = Sc["B"]
if R == 0 or G == 0 or B == 0:
    print(0)
    sys.exit()
else:
    ans = R*G*B
for i in range(1, N//2+2):
    for j in range(N):
        if j+i+i >= N:
            break
        if S[j] == S[j+i]:
            continue
        if S[j] == S[j+i+i]:
            continue
        if S[j+i] == S[j+i+i]:
            continue
        ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N = I()
S = [''] + LS2()

R = [0]*(N+1)
G = [0]*(N+1)
B = [0]*(N+1)
for i in range(1,N+1):
    s = S[i]
    R[i] = R[i-1]
    G[i] = G[i-1]
    B[i] = B[i-1]
    if s == 'R':
        R[i] += 1
    elif s == 'G':
        G[i] += 1
    else:
        B[i] += 1

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

    elif S[i] == 'G':
        ans += R[i-1]*(B[-1]-B[i])
        ans += B[i-1]*(R[-1]-R[i])
        for j in range(1,N+1):
            if 1 <= i-j <= N and 1 <= i+j <= N:
                if (S[i-j] == 'R' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'R'):
                    ans -= 1
    else:
        ans += G[i-1]*(R[-1]-R[i])
        ans += R[i-1]*(G[-1]-G[i])
        for j in range(1,N+1):
            if 1 <= i-j <= N and 1 <= i+j <= N:
                if (S[i-j] == 'R' and S[i+j] == 'G') or (S[i-j] == 'G' and S[i+j] == 'R'):
                    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 - 2):
    interval = 1
    while i + 2 * interval < n:
        if s[i] != s[i + interval] and s[i] != s[i + 2 * interval] and s[i + interval] != s[i + 2 * interval]:
            ans -= 1
        interval += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input().strip()
C = {"R":[],"G":[],"B":[]}
for i in range(N):
    C[S[i]].append(i)
C["B"] = set(C["B"])
cnt = 0
for r in C["R"]:
    for g in C["G"]:
        if r<g:
            i = r
            j = g
        else:
            i = g
            j = r
        cnt += len(C["B"])
        if i-(j-i) in C["B"]:
            cnt -= 1
        if j+(j-i) in C["B"]:
            cnt -= 1
        if (j+i)%2==0 and (j+i)//2 in C["B"]:
            cnt -= 1
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
cnt=0
for i in range(N):
    left=i-1
    right=i+1
    while 0<=left and right<N:
        if S[i]!=S[left] and S[i]!=S[right] and S[left]!=S[right]:
            cnt+=1
        left-=1
        right+=1
x=S.count('R')
y=S.count('G')
z=S.count('B')
print(x*y*z-cnt)

Submission status: Accepted

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

i = 1
count = 0
while i * 2 < N:
    n = 0
    while n + i * 2 < N:
        if (S[n] != S[n+i]) and (S[n+i] != S[n+2*i]) and (S[n] != S[n+i*2]):
            count += 1
            #print(S[n], S[n+i], S[n+2*i])
        n += 1
    i += 1

print(S.count('G') * S.count('R') * S.count('B') - count)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    if n < 4:
        print(0)
        return
    ans = s.count("R") * s.count("G") * s.count("B")

    for i, si in enumerate(s[:-2]):
        for j, sj in enumerate(s[i+1:-1]):
            if si == sj:
                continue

            if i + 2*j+2 < n and si != s[i + 2*j+2] != sj:
                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

R_list = [0]*N
G_list = [0]*N
B_list = [0]*N

for key,val in enumerate(S):
    if val=='R':
        R_list[key] = R_list[key-1]+1
        G_list[key] = G_list[key-1]
        B_list[key] = B_list[key-1]
    elif val=='G':
        R_list[key] = R_list[key-1]
        G_list[key] = G_list[key-1]+1
        B_list[key] = B_list[key-1]
    elif val=='B':
        R_list[key] = R_list[key-1]
        G_list[key] = G_list[key-1]
        B_list[key] = B_list[key-1]+1

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            continue
        a=2*(j+1)-(i+1)-1
        check = [S[i],S[j]]
        if 'R' not in check:
            ans += R_list[-1]-R_list[j]
            if a<N and S[a]=='R':
                ans -= 1
        elif 'G' not in check:
            ans += G_list[-1]-G_list[j]
            if a<N and S[a]=='G':
                ans -= 1
        elif 'B' not in check:
            ans += B_list[-1]-B_list[j]
            if a<N and S[a]=='B':
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import collections
import sys
import math
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())
def NIJIGEN(H): return [list(input()) for i in range(H)]
N=INT()
S=input()
if N<3:
  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):
    k=j*2-i
    if k>=N:
      continue
    if S[j]!=S[i] and S[k]!=S[i] and S[j]!=S[k]:
      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')
cnt=r*g*b
for i in range(n):
    for a in range(1,n):
        j=i+a
        k=j+a
        if k>=n:
            break
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            cnt-=1

print(cnt)

Submission status: Accepted

Solution code:

def main():
    '''
    #このコードだと、tleになる
    n = int(input())
    s = input()
    cnt = 0
    for i in range(len(s)):
        x = s[i]
        for j in range(i+1, len(s)):
            y = s[j]
            if x == y:
                continue
            for k in range(j+1, len(s)):
                z = s[k]
                if z == x or z == y:
                    continue
                if j - i == k - j:
                    continue
                cnt += 1
                # print(f"{i} {j} {k}")
    print(cnt)
    '''
    n = int(input())
    s = input()
    cnt = [0]*3
    ans = 1
    for i in s:
        if i == 'R':cnt[0]+=1
        elif i == 'G':cnt[1]+=1
        else:cnt[2]+=1
    for i in cnt:
        ans *= i
    for i in range(n):
        for j in range(i+1,n):
            k = j + (j - i)
            if k < n:
                if s[i] == s[j]: continue
                if s[j] == s[k]: continue
                if s[k] == s[i]: continue
                ans -= 1
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
import itertools
from functools import reduce
from functools import lru_cache


# \n
def input():
    return sys.stdin.readline().rstrip()

def main():

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

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

    count =0
    for i in range(N):



        for j in range(i+1,N):
            SE = set(["R", "G", "B"])
            I = S[i]
            SE.remove(I)
            J =S[j]
            if I==J:
                continue
            SE.remove(J)
            if "G" in SE:
                count+=G[j]
                if j+j-i <N and S[j+j-i] =="G":
                    count -=1

            elif "R" in SE:
                count+=R[j]
                if j+j-i <N and S[j+j-i] =="R":
                    count -=1
            else:
                count+=B[j]
                if j+j-i <N and S[j+j-i] =="B":
                    count -=1



    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
s,rem=input(),0
ans = s.count('R') * s.count('B') * s.count('G')

for i in range(1, (n-1) // 2 + 1):
  for j in range(i*2, n):
    if s[j]+s[j-i]+s[j-2*i] in ("RGB","RBG","GRB","GBR","BGR","BRG"):
      rem += 1
    
print(ans - rem)

Submission status: Accepted

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

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

res = r*g*b

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

print(res)

Submission status: Accepted

Solution code:
def inN():
    return int(input())
def inL():
    return list(map(int,input().split()))
def inNL(n):
    return [list(map(int,input().split())) for i in range(n)]


n = inN()
s = input()


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

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