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 math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    s = [ord(c) for c in S()]
    c = collections.defaultdict(int)
    r = 0
    c[s[0]] += 1
    m = sum(ord(c) for c in "RGB")
    for j in range(1,n):
        t = s[j]
        for k in range(j+1,n):
            u = s[k]
            if t == u:
                continue
            r += c[m-t-u]
            i = j - (k-j)
            if i >= 0 and s[i] == m-t-u:
                r -= 1

        c[t] += 1

    return r


print(main())

Submission status: Accepted

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

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

def counting(x):
  count = 0
  for i in range(1, math.ceil(x/2)):
    if s[x-1]!=s[x-1-i] and s[x-1-i]!=s[x-1-2*i] and s[x-1-2*i]!=s[x-1]:
      count += 1
    else:
      count += 0
  return count

if n>=3:
  minus = [0]*(n+1)
  if s[0]!=s[1] and s[1]!=s[2] and s[2]!=s[0]:
    minus [3] = 1
  else:
    minus [3] = 0
  for i in range(3,n):
    minus[i+1] = minus[i]+counting(i+1)
  print(int(upper-minus[n]))
else:
  print(int(0))

Submission status: Accepted

Solution code:
import itertools
import bisect

def main():
    N = int(input())
    S = input()
    d = {'R': [], 'G': [], 'B': []}
    for i, c in enumerate(S):
        d[c].append(i)
    #print(d)
    #answer = set()
    cnt = 0
    for xx, yy, zz in itertools.permutations(('R', 'G', 'B'), 3):
        x, y, z = d[xx], d[yy], d[zz]
        for i in x:
            y_start = bisect.bisect_left(y, i)
            for jj in range(y_start, len(y)):
                j = y[jj]
                assert i < j
                z_start = bisect.bisect_left(z, j)
                z_point = bisect.bisect_left(z, 2*j-i)
                #print(z_point, z)
                if (len(z) <= z_point) or (z[z_point] - j != j - i):
                    cnt += len(z) - z_start
                else:
                    cnt += len(z) - z_start - 1
                # for kk in range(z_start, len(z)):
                #     k = z[kk]
                #     assert j < k
                #     if k-j > j-i:
                #         #超えたのでもうチェックしなくていい
                #         cnt += len(z) - kk
                #         break
                #     elif k-j == j-i:
                #         cnt += len(z) - kk - 1
                #         break
                #     else:
                #         #assert k > j > i
                #         #answer.add((i+1, j+1, k+1))
                #         cnt += 1
    print(cnt)
    #assert len(answer) == cnt
    #print(answer)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r_num = s.count('R')
g_num = s.count('G')
b_num = s.count('B')
tot = r_num*g_num*b_num
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[i]!=s[k] and s[j] != s[k]:
                tot -= 1

print(tot)

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-1):
    for j in range(i+1, N):
        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:
import itertools
N = int(input().strip())
S = input().strip()

X = set()
Y = set()
Z = set()

# X, Y, Zの位置を挿入
for index in range(len(S)):
    if S[index] == 'R':
        X.add(index)
    elif S[index] == 'G':
        Y.add(index)
    elif S[index] == 'B':
        Z.add(index)

all_combination_num = len(X) * len(Y) * len(Z)
exclude_num = 0

rgbcombination = list(itertools.permutations('RGB', 3))
for i in range(1, N):
    for j in range(N - i * 2):
        if (S[j], S[i + j], S[j + i * 2]) in rgbcombination:
            exclude_num += 1

print(all_combination_num - exclude_num)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
d = [0,0,0]
for i in s:
    if(i == 'R'):d[0] += 1
    elif(i == 'G'):d[1] += 1
    elif(i == 'B'):d[2] += 1
c = d[0] * d[1] * d[2]

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]):continue
        if(s[j] == s[k]):continue
        if(s[i] == s[k]):continue
        c -= 1

print(c)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

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

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

INF = float('inf')

def solve():
    n = II()
    S = list(input())

    R = []
    G = []
    B = []
    for idx, s in enumerate(S):
        if s == 'R':
            R.append(idx)
        elif s == 'G':
            G.append(idx)
        else:
            B.append(idx)
    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:
                continue
            # print(i, j ,k)
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N=int(input())
S=input()
# N=int(Input[0][0])
# S=Input[1][0]
import math
count=0
counter={'R':0,'G':0,'B':0}
for i in S:
    counter[i]+=1
s=counter['R']*counter['G']*counter['B']
for i in range(1,N-1):
    for j in range(i+1,N):
        k=2*j-i
        if k<=N and (S[i-1]!=S[j-1] and S[i-1]!=S[k-1] and S[j-1]!=S[k-1]):
                count+=1
print(s-count)

Submission status: Accepted

Solution code:
"""
j−i != k−j
の条件がなければ、Rの数*Gの数*Bの数(*1)が答え

そこでまず*1を求めて、次に
j-i == k-j
となるような場合の数を求め、*1から引いたものが答え
"""

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
n = int(readline())
s = readline()

a = [0]*n
for i in range(n):
    if s[i] == 'R': a[i] = 0
    if s[i] == 'G': a[i] = 1
    if s[i] == 'B': a[i] = 2

cnt = [0]*3
for i in range(n):
    cnt[a[i]] += 1
    
ans = 1
for i in range(3):
    ans *= cnt[i]

for j in range(n):
    for i in range(j):
        k = j + (j-i)
        if k < n:
            if a[i] == a[j]: continue
            if a[i] == a[k]: continue
            if a[k] == a[j]: continue
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def binary_search(li,item):#二分探索
	left,right = 0, len(li)
	while left<=right:
		mid = (left+right)//2
		if li[mid]==item:
			return mid
		elif li[mid]<item:
			left = mid+1
		else:
			right = mid-1
	return -1

N = readInt()
S = readChar()
rgb = {"R":[],"G":[],"B":[]}
for i in range(N):
	rgb[S[i]].append(i)

c=s=0
c=len(rgb["R"])*len(rgb["G"])*len(rgb["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[i]!=S[k] and S[k]!=S[j]:
			s+=1

ans=c-s
print(ans)

Submission status: Accepted

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

count=0
rgb={'R':0, 'G':0, 'B':0}
for i in range(n):
  rgb[s[i]]+=1

count=rgb['R']*rgb['G']*rgb['B']

if n<=3:
  print(0)
  exit()

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

print(count)

Submission status: Accepted

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

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

print(total)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
s = str(stdin.readline().rstrip())
#l = list(map(int, stdin.readline().rstrip().split()))
#A, B, C = map(int, stdin.readline().rstrip().split())
#S = [list(map(int, stdin.readline().rstrip().split())) for _ in range(h)]#hの定義を忘れずに

rnum = s.count("R")
gnum = s.count("G")
bnum = s.count("B")

ans = rnum * gnum * bnum

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

for j in range(n):
    m=min(j,n-j-1)
    if m==0:
        continue
    for ik in range(m):
        ii=s[j-ik-1]
        jj=s[j]
        kk=s[j+ik+1]
        if (ii!=jj)&(jj!=kk)&(kk!=ii):ans-=1
print(ans)

Submission status: Accepted

Solution code:
# D - RGB Triplets

N = int(input())
S = input()
cnt_R = S.count('R')
cnt_G = S.count('G')
cnt_B = S.count('B')
total = cnt_R * cnt_G * cnt_B

expt = 0
for i in range(N - 2):
    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]):
            expt += 1

print(total - expt)

Submission status: Accepted

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

for i in range(N-1):
  for j in range(i,N):
    if 0 <= 2*i-j < N:
      if S[2*i-j] != S[i] != S[j] != S[2*i-j]:
        all -= 1
print(all)

Submission status: Accepted

Solution code:
m=int(input())
N=input()
cnt=N.count('R')*N.count('G')*N.count('B')
#print(cnt)
for i in range(m-2):
  #print(i,'*',int((m+i)/2))
  for j in range(i+1,int((m+i-1)/2)+1):
    k=2*j-i
    #print(i,j,k)
    if N[k] != N[j] and N[j] != N[i] and N[i] != N[k]:
      #print(i,j,k)
      cnt-=1
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
from copy import copy

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

total_cnt = dict(Counter(s))
if len(total_cnt) < 3:
    print(0)
    exit()

cnt = [total_cnt]
for i, c in enumerate(s):
    curr_cnt = copy(cnt[-1])
    curr_cnt[c] -= 1
    cnt.append(curr_cnt)

res = 0
for i, c1 in enumerate(s):
    for j in range(i + 1, n - 1):
        c2 = s[j]
        if c2 == c1:
            continue
        for c3 in "RGB":
            if c3 == c1 or c3 == c2:
                continue
            res += cnt[j + 1][c3]
            k = 2 * j - i
            if k >= n:
                continue
            if s[k] == c3:
                res -= 1
print(res)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
ans=0
tmp=0
for k in range(N):
  for j in range(k):
    i=2*j-k
    if i>=0:
      if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
        tmp+=1
ans=S.count("R")*S.count("G")*S.count("B")-tmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

Submission status: Accepted

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

cnts = [0,0,0]
for c in S:
	if c == 'R':
		cnts[0] = cnts[0] + 1 
	if c == 'G':
		cnts[1] = cnts[1] + 1 
	if c == 'B':
		cnts[2] = cnts[2] + 1 

ans = cnts[0] * cnts[1] * cnts[2]

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    s = input()

    ans = 1
    for c in 'RGB':
        ans *= s.count(c)

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

    print(ans)


if __name__ == '__main__':
    main()

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, N):
        k = 2*j - i
        if N > k 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 = s.count("R")
G = s.count("G")
B = s.count("B")

c1 = R*G*B

c1_2bar = 0

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

print(c1 - c1_2bar)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    N = int(input())
    S = input()
    if N < 4:
        print(0)
        exit(0)
    rgb_cnt = defaultdict(int)
    for c in S:
        rgb_cnt[c] += 1
    # Caclulate cases that first condition is satisfied but second condision not,
    # then remove this cases from first condition cases finally.
    cnt = 0
    for center in range(N):
        cc = S[center]
        for w in range(1, N + 1):
            left = center - w
            right = center + w
            if left < 0 or right > N - 1:
                continue
            cl = S[left]
            cr = S[right]
            if cl != cc and cc != cr and cr != cl:
                cnt += 1
    answer = rgb_cnt['R'] * rgb_cnt['G'] * rgb_cnt['B'] - cnt
    print(answer)


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#条件1を満たす組み合わせの数
for i in range(1,(N-1)//2 +1):
    for j in zip(S,S[i:],S[2*i:]):#jには(S[j],S[i+j],S[2*i+j])がタプルで収納される
        if len(set(j))==3:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
import math
import collections

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

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

    count = 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]):
                    count += 1

    print(c - count)

    return 0

if __name__ == '__main__':
    main()

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-2):
  for j in range(i+1, N-1):
    k = 2*j-i
    if k <= N-1:
      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()
ans = s.count("R") * s.count("G") * s.count("B")

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

print(ans)

Submission status: Accepted

Solution code:
import statistics as st
n = int(input())
s = input()
NGs = 0
r = set()
g = set()
b = set()
for i in range(n):
  if s[i] == "R":
    r.add(i)
  elif s[i] == "G":
    g.add(i)
  else:
    b.add(i)
for i in range(n-2):
  for d in range((n-i-1)//2+1):
    j = i+d
    k = j+d
    if i in r and j in g and k in b:
      NGs += 1
    elif i in r and j in b and k in g:
      NGs += 1
    elif i in g and j in r and k in b:
      NGs += 1
    elif i in g and j in b and k in r:
      NGs += 1
    elif i in b and j in r and k in g:
      NGs += 1
    elif i in b and j in g and k in r:
      NGs += 1
print(len(r)*len(g)*len(b)-NGs)

Submission status: Accepted

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

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

ans = 0

# 1つ目の条件を満たす組の総数は
all_1 = r * g * b

# 1つ目の条件を満たすかつ2つ目の条件を満たさない組の個数
for i in range(N):
    for j in range(i + 1, N):
        k = 2*j - i
        if k >= N:
            continue
        if (S[i] is not S[j]) and (S[i] is not S[k]) and (S[j] is not S[k]):
            ans += 1

s = all_1 - ans

print(s)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [i for i in input()]

r = 0
g = 0
b = 0
no = 0

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

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]:
            no += 1
 
print(r*g*b - no)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = [i for i in input()]
count = 0
s = Counter(S)
for i in range(N):
    for j in range(i,N):
        if (i+j)%2==0 and S[(i+j)//2]!=S[i] and S[i]!=S[j] and S[(i+j)//2]!=S[j]:
            count += 1

print((s['R'] * s['B'] * s['G'])-count)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
s=input()

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

for i in range(0,n):
    if s[i]=='R':
        r.append(i)

    elif s[i]=='G':
        g.append(i)

    elif s[i]=='B':
        b.append(i)

s3=0
rl=len(r)
gl=len(g)
bl=len(b)

for i in range(0,n-2):
    n2=int((n+i)/2)+1
    for j in range(i,n2):
        k=2*j-i
        if k<=n-1:
            
            if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                s3+=1
s2=rl*gl*bl-s3
print(s2)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = list(input())
    count = {"G": 0, "R":0, "B":0}
    for i in s:
        count[i] += 1
    ans = count["G"]*count["R"]*count["B"]
    for j in range(n):
        for i in range(1, n//2+1):
            if j+2*i > n-1:
                continue
            if s[j] != s[j+i] and s[j+i] != s[j+i+i] and s[j] != s[j+i+i]:
                ans -= 1
    print(ans)
    
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
s=list(input())
ans=0
t=[]
ss=[]
for i in range(n):
    t.append(s[i:])
    ss.append(t[i].count('R'))
    ss.append(t[i].count('G'))
    ss.append(t[i].count('B'))
#print(t)
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i]==s[j]:
            continue
        else:
            if s[i]=='R' and s[j]=='G'or s[i]=='G'and s[j]=='R':
                ans+=ss[3*(j+1)+2]
                if 2*j-i<n:
                    if s[2*j-i]=='B':
                        ans-=1
            if s[i]=='B' and s[j]=='R'or s[i]=='R'and s[j]=='B':
                ans+=ss[3*(1+j)+1]
                if 2*j-i<n:
                    if s[2*j-i]=='G':
                        ans-=1
            if s[i]=='B' and s[j]=='G'or s[i]=='G'and s[j]=='B':
                ans+=ss[3*j+3]
                if 2*j-i<n:
                    if s[2*j-i]=='R':
                        ans-=1
        #print(ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s_list = list(str(input()))
r = 0
g = 0
b = 0

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

ans = r * g * b

for d in range(1, n):
    for i in range(n):
        if i + 2 * d >= n:
            break
        if s_list[i] != s_list[i + d] and \
            s_list[i] != s_list[i + 2 * d] and \
            s_list[i + d] != s_list[i + 2 * d]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = input()
c = collections.Counter(s)
raw = c['R'] * c['G'] * c['B']

for i in range(n):
    for j in range(i+1, n):
        if s[i] != s[j]:
            step = j - i
            k = j + step
            try:
                m = s[k]
            except:
                break
            if s[i] != m and s[j] != m:
                raw -= 1
print(raw)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = 0
g = 0
b = 0
for letter in s:
    if letter == "R":
        r +=1
    elif letter == "G":
        g +=1
    elif letter == "B":
        b +=1
total = r * g * b
subs = 0
for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k <= n - 1:
            if s[i] == s[j]:
                continue
            elif s[j] == s[k]:
                continue
            elif s[k] == s[i]:
                continue
            else:
                subs += 1
        else:
            break
ans = total - subs
print(ans)

Submission status: Accepted

Solution code:
import itertools

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

r_cnt = S.count("R")
g_cnt  = S.count("G")
b_cnt = S.count("B")

ans = r_cnt * g_cnt * b_cnt

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

Submission status: Accepted

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

rcnt = 0
gcnt = 0
bcnt = 0

for c in S:
    if c == 'R':
        rcnt += 1
    elif c == 'G':
        gcnt += 1
    elif c == 'B':
        bcnt += 1

ans = rcnt * gcnt * bcnt
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 = str(input())
rn = s.count("R")
gn = s.count("G")
bn = s.count("B")
 
ct=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[j]!=s[k]and s[i]!=s[k]:
      ct += 1
 
print(rn*gn*bn-ct)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
rn = s.count("R")
gn = s.count("G")
bn = s.count("B")

ct=0
for i in range(n):
  for j in range(i+1,n):
    if 2*j - i >= n:
      break
    elif s[i]!=s[j] and s[j]!=s[2*j-i] and s[i]!=s[2*j-i]:
      ct += 1
 
print(rn*gn*bn-ct)

Submission status: Accepted

Solution code:
import math

n = int(input())
s = input()
ex = 0

for i in range(n-2):
  for x in range(1, int(math.ceil((n-i)/2))):
    if s[i] != s[i+x] and s[i] != s[i+2*x] and s[i+x] != s[i+2*x]:
      ex += 1

print(s.count("R") * s.count("G") * s.count("B")-ex)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

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

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

INF = float('inf')

def solve():
    n = II()
    S = list(input())
    # print(S)
    n = len(S)

    R = []
    G = []
    B = []
    rcnt = 0
    gcnt = 0
    bcnt = 0
    for idx, s in enumerate(S):
        if s == 'R':
            R.append(idx+1)
            rcnt += 1
        elif s == 'G':
            G.append(idx+1)
            gcnt += 1
        else:
            B.append(idx+1)
            bcnt += 1
    # print(R)
    # print(G)
    # print(B)
    ans = rcnt * gcnt * bcnt
    # print(ans)

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





if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

def Solve(N:int,S:str) -> int:
    total = Counter(S)
    counter1 = {"R":0,"G":0,"B":0}
    ans = 0

    for i in range(N-2):
        RGB = {"R","G","B"} - {S[i]}
        counter1[S[i]]+=1
        counter2 = {"R":0,"G":0,"B":0}
        for j in range(i+1,N-1):
            counter2[S[j]]+=1
            tmp = list(RGB - {S[j]})
            if len(tmp)==1:
                target = tmp[0]
                ans+=total[target]-counter1[target]-counter2[target]
                if((j - i)+ j <= N -1 and S[(j - i)+ j] == target):
                    ans-=1
    return ans

def main():
    N = int(input())
    S = input()
    print(Solve(N,S))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = list(input())
dic = Counter(S)
dic2 = {"R":0,"G":0,"B":0}
ans = 0
for i in range(N-2):
    RGB = {"R","G","B"} - {S[i]}
    dic2[S[i]]+=1
    dic3 = {"R":0,"G":0,"B":0}
    for j in range(i+1,N-1):
        dic3[S[j]]+=1
        if len(RGB - {S[j]})!=1:
            continue
        else:
            target = list(RGB - {S[j]})[0]
            ans+=dic[target]-dic2[target]-dic3[target]
            if((j - i)+ j <= N -1):
                if(S[(j - i)+ j] == target):
                    ans-=1
print(ans)

Submission status: Accepted

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

for n in range(N):
    if S[n]=="R":
        R.append(n)
    elif S[n]=="G":
        G.append(n)
    else:
        B.append(n)

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

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


print(ANS)

Submission status: Accepted

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

pls = S.count("R") * S.count("G") * S.count("B")
minus_count = 0
for i in range(len(S)):
    for j in range(i + 1, len(S)):
        if S[i] == S[j]:
            continue
        dis = j - i
        k = j + dis
        if k > len(S) - 1:
            break
        if S[i] == S[k]:
            continue
        elif S[j] == S[k]:
            continue
        minus_count += 1
print(pls - minus_count)