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:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N = int(readline())
    S = readline().strip()

    # 各色について累積和を取る
    R = [0]
    G = [0]
    B = [0]
    for i, s in enumerate(S):
        if s == "R":
            R.append(R[-1] + 1)
            G.append(G[-1])
            B.append(B[-1])
        elif s == "G":
            R.append(R[-1])
            G.append(G[-1] + 1)
            B.append(B[-1])
        else:
            R.append(R[-1])
            G.append(G[-1])
            B.append(B[-1] + 1)

    ans = 0
    for i, s in enumerate(S, start=1):
        if s == "R":
            ans += G[i-1] * (B[-1] - B[i])
            ans += B[i-1] * (G[-1] - G[i])
            for j in range(i):
                if S[j] == "G":
                    if 2*i-j-2 < N and S[2*i-j-2] == "B":
                        ans -= 1
                if 2*i-j-2 < N and S[j] == "B":
                    if S[2*i-j-2] == "G":
                        ans -= 1
        
        elif s == "G":
            ans += R[i-1] * (B[-1] - B[i])
            ans += B[i-1] * (R[-1] - R[i])
            for j in range(i):
                if S[j] == "R":
                    if 2*i-j-2 < N and S[2*i-j-2] == "B":
                        ans -= 1
                if 2*i-j-2 < N and S[j] == "B":
                    if S[2*i-j-2] == "R":
                        ans -= 1

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans, gap = S.count('R') * S.count('G') * S.count('B'), 1
while gap * 2 + 1 <= N:
    ans -= sum([1 for i in range(N - gap * 2)
                if {'R', 'G', 'B'} == {S[i], S[i + gap], S[i + 2 * gap]}])
    gap += 1
print(ans)

Submission status: Accepted

Solution code:
def func1(x, y, z, a):
  if x == y:
    z.append(a)
    return 1 
  return 0

def func2(x, y):
  if x==y:
    return 1
  return 0

def func3(x, y, z, a):
  l = 0
  for i in x:
    for j in y:
      k = 2 * j - i
      if k <= N-1 and i < j:
        l += func2(z[k], a)
  return l

  
N = int(input())
S = input()
r, g, b, rgb, equal = 0, 0, 0, 0, 0
r0, g0, b0 = [], [], []

for i in range(N):
  r += func1(S[i], 'R', r0, i)
  g += func1(S[i], 'G', g0, i)
  b += func1(S[i], 'B', b0, i)

rgb = r * g * b

# R<G<B
equal += func3(r0, g0, S, 'B')
# R<B<G
equal += func3(r0, b0, S, 'G')
# G<R<B
equal += func3(g0, r0, S, 'B')
# G<B<R
equal += func3(g0, b0, S, 'R')
# B<R<G
equal += func3(b0, r0, S, 'G')
# B<G<R
equal += func3(b0, g0, S, 'R')

print(rgb - equal)

Submission status: Accepted

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

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

ans = r_cnt * g_cnt * b_cnt

for i in range(n):      #s[i]
    for d in range(n):
        j = i + d       #s[j]　s[i]etcこれらは等間隔となるが
        k = j + d       #s[k]　この時点でi!=j, j!=k, i!=k
        if k >= n:break #kが一番大きいが、n以上になることはない
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1    #等間隔でRGB揃っているものを引く

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
 
count = {"R":0,"G":0,"B":0}
for c in s:
    count[c] += 1
 
r,g,b = count.values()
ans = r*g*b
for i in range(1,(n-1)//2+1): #1から
    for rgb in zip(s,s[i:],s[i*2:]):  #rgbに連続した三つのリストの要素を入れ込む
        if len(set(rgb)) == 3:  #リストrgbをセットにまとめ、長さが
            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')

count = 0

for i in range(1, N+1):
  for j in range(1, min(i,N-i-1)+1):
    if S[i-j] != S[i] and S[i+j] != S[i] and S[i+j] != S[i-j]:
      count+=1
      
print((r * g * b) - count)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
d = defaultdict(int)

n = int(input())
s = input()
r=[]
g=[]
b=[]
for i in range(n):
  #print(s[i])
  if s[i] == 'R':
    r.append(i)
  elif s[i] == 'G':
    g.append(i)
  else:
    #b.append(i)
    d[i] = 1

#print(r)
#print(g)
#print(b)
sum = len(r)*len(g)*len(d)
for i in r:
  for j in g:
    ma = max(i,j)
    mi = min(i,j)
    df = ma - mi
    k1 = mi - df
    k2 = ma + df
    k3 = (ma + mi) / 2
    sum -= d[k1]
    sum -= d[k2]
    sum -= d[k3]
print(sum)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
lsR=[]
lsG=[]
lsB=[]

for i in range(N):
  if S[i]=="R":
    lsR.append(i)
  elif S[i]=="G":
    lsG.append(i)
  else:
    lsB.append(i)
a=len(lsR)*len(lsG)*len(lsB)
b=0
for i in range(N):
  for j in range(i+1,N):
    if S[i]!=S[j]:
      k=2*j-i
      if k<N:
        if S[i]!=S[k] and S[j]!=S[k]:
          b+=1
print(a-b)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    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(i, n):
            k = 2 * j - i
            if 0 <= k < n:
                if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                    res -= 1

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = len(list(filter(lambda c: c == 'R', s)))
g = len(list(filter(lambda c: c == 'G', s)))
b = len(list(filter(lambda c: c == 'B', s)))
ans = r * g * b - sum([sum([1 for j in range(i + 1, (n + i + 1) // 2) if s[i] != s[j] and s[j] != s[(k := 2 * j - i)] and s[k] != s[i]]) for i in range(n)])
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
c = 0
rgb = 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:
      if (S[i] != S[j] and S[i] != S[2*j-i] and S[j] != S[2*j-i]):
        c+=1
print(rgb-c)

Submission status: Accepted

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

cnt = s.count("R")*s.count("G")*s.count("B")
for i in range(0, n):
    for j in range(i+1, n):
        k = 2*j - i
        if s[i] == s[j] or k >= n or s[k] == s[i] or s[k] == s[j]:
            continue
        else:
            cnt -= 1

print(cnt)

Submission status: Accepted

Solution code:
import re

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

R = re.findall("R", S)
G = re.findall("G", S)
B = re.findall("B", S)

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

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

Submission status: Accepted

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

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

for fst_idx in range(n):
    for scd_idx in range(fst_idx + 1, n):
        trd_idx = scd_idx * 2 - fst_idx
        if trd_idx >= n:
            break
        elif s[fst_idx] != s[scd_idx] and s[scd_idx] != s[trd_idx] and \
                s[trd_idx] != s[fst_idx]:
            result -= 1

print(result)

Submission status: Accepted

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

from collections import Counter

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

for step in range(1,n//2+2):
    for start in range(n):
#         print(start,step,start+2*step,n)
        if start+2*step>=n:
#             print("out!")
            break
        ans-=len(set((s[start], s[start+step],s[start+2*step])))==3
print(ans)

Submission status: Accepted

Solution code:
from time import time 
N = int(input())
S = str(input())
t0 = time()
x, y, z = 0,0,0
for i in range(N):
    if S[i] == "R":
        x += 1
    elif S[i] == "G":
        y += 1
    elif S[i] == "B":
        z += 1
    else:
        print("ERROR")

no = 0
for s in range(N-2):
    d = 1
    while(s+2*d < N):
#         if len(set([S[s],S[s+d],S[s+2*d]])) == 3:
        a = S[s]
        b = S[s+d]
        c = S[s+2*d]
        if a != b and b != c and c != a:
            no += 1
        d += 1

print(x*y*z-no)
# print(time() - t0)

Submission status: Accepted

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

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

k = 0
count = 0
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if(i>=j or j >=k or i >=k): continue
        if(i==j or j==k or k==i or k<0 or k>=N): continue
        if(S[i] == S[j] or S[j] == S[k] or S[k] == S[i]): continue
        
        count += 1
        
print(total - count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

k = int(input())
n = input()

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

ans = r*g*b

for i in range(k):
    
    for j in range(i + 1, math.ceil((k + i)/2)):
        
        if (n[i] != n[j]) and (n[2*j - i] != n[i]) and (n[j] != n[2*j - i]):
            ans -= 1

print(ans)

Submission status: Accepted

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

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

ans = r_cnt * g_cnt * b_cnt

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

print(ans)

Submission status: Accepted

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

#output
import numpy as np
answer = 0
R = np.zeros(N, int)
G = np.zeros(N, int)
B = np.zeros(N, int)
for i in range(N):
    if S[i] == "R":
        R[i] = 1
    elif S[i] == "G":
        G[i] = 1
    elif S[i] == "B":
        B[i] = 1
R = np.cumsum(R)
G = np.cumsum(G)
B = np.cumsum(B)
RR = R[-1]
GG = G[-1]
BB = B[-1]

if N > 3:
    for i in range(1, N-1):
        if S[i] == "R":
            a = G[i]
            b = BB - B[i]
            c = B[i]
            d = GG - G[i]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "R" and S[i+j] != "R" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "G":
            a = B[i]
            b = RR - R[i]
            c = R[i]
            d = BB - B[i]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "G" and S[i+j] != "G" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "B":
            a = R[i]
            b = GG - G[i]
            c = G[i]
            d = RR - R[i]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "B" and S[i+j] != "B" and S[i-j] != S[i+j]:
                    answer -= 1
        
print(answer)

Submission status: Accepted

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

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    from itertools import permutations
    n = ii()
    s = input()
    ind_lst = [[] for _ in range(3)]
    d = {'R':0, 'G':1, 'B':2}
    for i, char in enumerate(s):
        ind_lst[d[char]].append(i)
    cnt = 0
    # for perm in permutations([0, 1, 2]):
    #     a, b, c = perm
    #     for x in ind_lst[a]:
    #         i = bisect_left(ind_lst[b], x)
    #         for y in ind_lst[b][i:]:
    #             j = bisect_left(ind_lst[c], y)
    #             for z in ind_lst[c][j:]:
    #                 if x + z != 2 * y:
    #                     cnt += 1
    rest_set = set(ind_lst[2])
    num_rest = len(ind_lst[2])
    for x in ind_lst[0]:
        for y in ind_lst[1]:
            decreased = 0
            u, v = min(x, y), max(x, y)
            if 2 * u - v in rest_set:
                decreased += 1
            if (u + v) % 2 == 0 and (u + v) // 2 in rest_set:
                decreased += 1
            if 2 * v - u in rest_set:
                decreased += 1
            cnt += num_rest - decreased
            
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

dic = Counter(S)
ans = dic["R"] * dic["G"] * dic["B"]

for cntr in range(N):
    i = 1
    while True:
        left = cntr - i
        right = cntr + i
        if not (0 <= left and right < N): break
        if S[left] != S[cntr] and S[cntr] != S[right] and S[right] != S[left]: ans -= 1
        i += 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")

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


print(r*g*b-an)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
s = input()
R = []
G = []
B = []
for i in range(n):
    if s[i] == "R":
        R.append(i)
    elif s[i] == "G":
        G.append(i)
    else:
        B.append(i)
# 全体R*G*Bからi,jを固定してk = 2*i-jが全部違う数字になるものを引けばいい
ans = len(R) * len(G) * len(B)
for i in range(n):
    for j in range(i + 1, n):
        if 0 <= 2 * j - i and 2 * j - i < n:
            if s[2 * j - i] != s[i] and s[2 * j - i] != s[j] and s[i] != s[j]:
                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 j in range(i+2, n, 2):
    if s[i] != s[j] and s[i] != s[int((i+j)/2)] and s[int((i+j)/2)] != s[j]:
      ans -= 1
print(ans)

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
minus = 0
for i in range(N - 2):
  for j in range(i + 1, N - 1):
    k = 2 * j - i
    if k >= N:
      continue
    if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
      minus += 1
print(r*g*b - minus)

Submission status: Accepted

Solution code:
import math

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

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

for i in range(n):
    for j in range(n):
        c1 = i
        c2 = i + j
        c3 = i + j * 2
        if c3 >= n: break
        if c1 == c2 == c3: continue
        if s[c1] != s[c2] and s[c2] != s[c3] and s[c1] != s[c3]:
            ans -= 1

print(ans)
# 012 024 036 048
# 123 135 147 159

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ct=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:
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                ct-=1
print(ct)

Submission status: Accepted

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

n = int(input())
s = input() #sys.stdin.readlineは最後が改行

r = [0]*n
g = [0]*n
b = [0]*n

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

from itertools import accumulate
r_nokori = list(reversed(r))
r_nokori = list(reversed(list(accumulate(r_nokori))))
g_nokori = list(reversed(g))
g_nokori = list(reversed(list(accumulate(g_nokori))))
b_nokori = list(reversed(b))
b_nokori = list(reversed(list(accumulate(b_nokori))))

ans = 0

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

        if (s[i]=='R' and s[j]=='G') or (s[i]=='G' and s[j]=='R'):
            ans += b_nokori[j+1]
            if 2*j-i <= n:
                if s[2*j-i] == 'B': ans-= 1

        if (s[i]=='G' and s[j]=='B') or (s[i]=='B' and s[j]=='G'):
            ans += r_nokori[j+1]
            if 2*j-i <= n:
                if s[2*j-i] == 'R': ans-= 1

        if (s[i]=='B' and s[j]=='R') or (s[i]=='R' and s[j]=='B'):
            ans += g_nokori[j+1]
            if 2*j-i <= n:
                if s[2*j-i] == 'G': ans-= 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
cnt = 0
cnt += s.count('R')*s.count('G')*s.count('B')
for i in range(1,n-1):
    if s[i] == 'R':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'G' and s[i+j]=='B':
                cnt -=1
            if s[i-j] == 'B' and s[i+j]=='G':
                cnt -=1
    if s[i] == 'G':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'R' and s[i+j]=='B':
                cnt -=1
            if s[i-j] == 'B' and s[i+j]=='R':
                cnt -=1
    if s[i] == 'B':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'G' and s[i+j]=='R':
                cnt -=1
            if s[i-j] == 'R' and s[i+j]=='G':
                cnt -=1
print(cnt)

Submission status: Accepted

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

s=input()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=r*g*b
for i in range(n):
    for j in range(i+1,n):
        k=2*j-i
        if k>=n:
            continue
        if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
            ans-=1
print(ans)

Submission status: Accepted

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

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

sum = r*g*b

for i in range(N):
    for j in range(N):
        try:
            a = S[i]
            b = S[i+j]
            c = S[i+2*j]
            if a!=b and b!=c and c!=a:
                sum -= 1
        except:
            break

print(sum)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N = int(input())
S = input()

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

count = r*g*b
#print(count)
for i in range(N - 1):
    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(count)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
count=0
r=S.count("R")
g=S.count("G")
b=S.count("B")

count+=r*g*b
#print(r,g,b)
for i in range(0,N-1):
	for j in range(i+1,N):
		k=2*j-i
		if k>=N:break
		if S[i]!=S[j] and S[j]!=S[k] and S[i]!=S[k]:
			count-=1
print(count)

Submission status: Accepted

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

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

ct = 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:
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                ct-=1
print(ct)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
r = [i for i, x in enumerate(s) if x == "R"]
g = [i for i, x in enumerate(s) if x == "G"]
b = [i for i, x in enumerate(s) if x == "B"]
ans = len(r) * len(g) * len(b)

for i in r:
    for j in g:
        i_ = min(i, j)
        j_ = max(i, j)
        div = (j_ - i_)
        k_1 = i_ - div
        k_2 = div // 2 + i_
        k_3 = div + j_
        if k_1 >= 0 and s[k_1] == "B":
            ans -= 1
        if div % 2 == 0 and s[k_2] == "B":
            ans -= 1
        if k_3 < n and s[k_3] == "B":
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = input()
cnt = Counter(S)
total = cnt["R"] * cnt["G"] * cnt["B"]
S = "#" + S

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

print(total - sub)

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(n):
    for j in range(i+1, n):
        k = 2 * j - i
        if k < n:
            if len({sl[i], sl[j], sl[k]}) ==  3:
                ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')
res = r*g*b
for i in range(n-1):
    for j in range(i+1,n):
        k = 2*j - i
        if k < n :
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                res -= 1
print(res)

Submission status: Accepted

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

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

ad=r*g*b
mi=0

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

print(ad-mi)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=g=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):
    k_ = j+(j-i)
    if(k_ >=n):
      break
    ans -= int(s[i]!= s[j] and s[i] != s[k_] and s[j] != s[k_])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s = s.replace('R', '0') 
s = s.replace('G', '1')
s = s.replace('B', '2')

t = [[0] * (n + 5) for _ in range(3)]

for i in range(n):
    for j in range(3):
        t[j][i + 1] = t[j][i] + (s[i] == chr(48 + j))

ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]: continue
        h = 99 - (ord(s[i]) + ord(s[j]))
        ans += t[h][n] - t[h][j]
        if 2 * j - i < n and s[2 * j - i] == chr(48 + h): ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s = s.replace('R', '0')
s = s.replace('G', '1')
s = s.replace('B', '2')

s0 = [0] * (n + 5)
s1 = [0] * (n + 5)
s2 = [0] * (n + 5)

for i in range(n):
    s0[i + 1] = s0[i] + (s[i] == '0')
    s1[i + 1] = s1[i] + (s[i] == '1')
    s2[i + 1] = s2[i] + (s[i] == '2')

#print(s0)
#print(s1)
#print(s2)

ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]: continue
        h = 99 - (ord(s[i]) + ord(s[j]))
        if h == 0:
            ans += s0[n] - s0[j]
            if 2 * j - i < n and s[2 * j - i] == '0': ans -= 1
        if h == 1:
            ans += s1[n] - s1[j]
            if 2 * j - i < n and s[2 * j - i] == '1': ans -= 1
        if h == 2:
            ans += s2[n] - s2[j]
            if 2 * j - i < n and s[2 * j - i] == '2': ans -= 1
        #print(i, j, h, ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
# B_map = set()
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

res = R * G * B

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

Submission status: Accepted

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

# RGBそれぞれの字数カウント
R = S.count('R')
G = S.count('G')
B = S.count('B')

# 条件２の対象数カウント
ad = R * G * B
ed = 0

for i in range(0, N-1):
  for j in range(i+1, N):
    # 判定
    P = j+(j-i)
    if P < N:
      if S[i] != S[j] and S[j] != S[P] and S[P] != S[i]:
        ed += 1

print(ad-ed)

Submission status: Accepted

Solution code:
from itertools import permutations as pm

def main():
    n = int(input())
    s = input()

    red = [0 for i in range(n+1)]
    green = [0 for i in range(n+1)]
    blue = [0 for i in range(n+1)]

    for i in range(2,n+1):
        if s[i-2]=='R':
            red[i] = red[i-1] + 1
        else:
            red[i] = red[i-1]
        if s[i-2]=='G':
            green[i] = green[i-1] + 1
        else:
            green[i] = green[i-1]
        if s[i-2]=='B':
            blue[i] = blue[i-1] + 1
        else:
            blue[i] = blue[i-1]
    # red[n] = red[n-1]
    # green[n] = green[n-1]
    # blue[n] = blue[n-1]

    # print(red)
    # print(blue)
    # print(green)


    ans = 0
    for i in range(1,n+1):
        for j in range(i+2,n+1):

            if s[i-1]+s[j-1] == "RB" or s[i-1]+s[j-1] =="BR":
                ans += green[j] - green[i+1]
                if (j-i)%2==0 and s[(j-i)//2+i-1] == "G":
                    ans -= 1
            elif s[i-1]+s[j-1] == "RG" or s[i-1]+s[j-1] =="GR":
                ans += blue[j] - blue[i+1]
                if (j-i)%2==0 and s[(j-i)//2+i-1] == "B":
                    ans -= 1
            elif s[i-1]+s[j-1] == "BG" or s[i-1]+s[j-1] =="GB":
                ans += red[j] - red[i+1]
                if (j-i)%2==0 and s[(j-i)//2+i-1] == "R":
                    ans -= 1

    print(ans)

if __name__== "__main__":
    main()

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

Submission status: Accepted

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

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

sum = r*g*b

for i in range(N):
    for j in range((N-i)//2 +1):
        try:
            a = S[i]
            b = S[i+j]
            c = S[i+2*j]
            if a!=b and b!=c and c!=a:
                sum -= 1
        except:
            continue

print(sum)

Submission status: Accepted

Solution code:
def checkRGB(a,b,c):
    if a == b or b == c or c == a:
        return 0
    else:
        return 1

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

if N<4:
    print(0)
    exit()
sum1 = S.count("R")*S.count("G")*S.count("B")
sum2 = 0
for i in range(0,N-2):
    left = S[i]
    for j in range(i+1,(N+i)//2+(N+i)%2):
        center = S[j]
        right = S[2*j-i]
        if checkRGB(left,center,right):
            sum2 += 1
print(sum1-sum2)

Submission status: Accepted

Solution code:
from itertools import permutations

def main():
    n = int(input())
    s = input()
    res = 0
    r = 0
    g = 0
    b = 0
    for i, c in enumerate(s):
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        else:
            b += 1
    
    rgb = []
    for t in permutations('RGB'):
        rgb.append(''.join(t))

    cnt = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            k = 2*j - i 
            if k >= n:
                break
            if (s[i] + s[j] + s[k]) in rgb:
                cnt += 1
    print(r*g*b - cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
s=input()
R=[0]
G=[0]
B=[0]
ans=0
#RGBの累積和を作る
for i in range(N):
  if s[i]=="R":
    R[i]+=1
  elif s[i]=="G":
    G[i]+=1
  else:
    B[i]+=1
  R.append(R[i])
  G.append(G[i])
  B.append(B[i])
del R[-1]
del G[-1]
del B[-1]
#作った
 
#全探索する
for i in range(N-2):
  a=s[i]
  for j in range(i+1,N-1):
    if a==s[j]:
      continue
    b=s[j]
    k=2*j-i
    if "R" not in a+b:
      ans+=R[-1]-R[j]
      if k>=N:
        continue
      if s[k]=="R":
        ans-=1
    
    if "G" not in a+b:
      ans+=G[-1]-G[j]
      if k>=N:
        continue
      if s[k]=="G":
        ans-=1

    if "B" not in a+b:
      ans+=B[-1]-B[j]
      if k>=N:
        continue
      if s[k]=="B":
        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):
  a=s[i]
  for j in range(i+1,N-1):
    if a==s[j]:
      continue
    b=s[j]
    k=2*j-i
    if k>=N:
      continue
    if s[k]!=a and s[k]!=b:
      ans-=1
print(ans)